]> git.sesse.net Git - vlc/blob - src/playlist/control.c
luasd: use a configuration chain to pass parameters to SD
[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 static vlc_mutex_t global_lock = VLC_STATIC_MUTEX;
43
44 #undef pl_Hold
45 playlist_t *pl_Hold (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
60     /* The playlist should hold itself with vlc_object_hold() if ever. */
61     assert (VLC_OBJECT (pl) != obj);
62     if (pl)
63         vlc_object_hold (pl);
64     vlc_mutex_unlock (&global_lock);
65     return pl;
66 }
67
68 #undef pl_Release
69 void pl_Release( vlc_object_t *p_this )
70 {
71     playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
72     assert( pl != NULL );
73
74     /* The rule is that pl_Release() should act on
75        the same object than pl_Hold() */
76     assert( VLC_OBJECT(pl) != p_this);
77
78     vlc_object_release( pl );
79 }
80
81 void pl_Deactivate (libvlc_int_t *p_libvlc)
82 {
83     vlc_mutex_lock (&global_lock);
84     if (libvlc_priv (p_libvlc)->playlist_active)
85         playlist_Deactivate (libvlc_priv (p_libvlc)->p_playlist);
86     vlc_mutex_unlock (&global_lock);
87 }
88
89 void playlist_Lock( playlist_t *pl )
90 {
91     vlc_mutex_lock( &pl_priv(pl)->lock );
92 }
93
94 void playlist_Unlock( playlist_t *pl )
95 {
96     vlc_mutex_unlock( &pl_priv(pl)->lock );
97 }
98
99 void playlist_AssertLocked( playlist_t *pl )
100 {
101     vlc_assert_locked( &pl_priv(pl)->lock );
102 }
103
104 int playlist_Control( playlist_t * p_playlist, int i_query,
105                       bool b_locked, ... )
106 {
107     va_list args;
108     int i_result;
109     PL_LOCK_IF( !b_locked );
110     va_start( args, b_locked );
111     i_result = PlaylistVAControl( p_playlist, i_query, args );
112     va_end( args );
113     PL_UNLOCK_IF( !b_locked );
114
115     return i_result;
116 }
117
118 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
119 {
120     playlist_item_t *p_item, *p_node;
121
122     PL_ASSERT_LOCKED;
123
124     if( !vlc_object_alive( p_playlist ) )
125         return VLC_EGENERIC;
126
127     if( playlist_IsEmpty( p_playlist ) && i_query != PLAYLIST_STOP )
128         return VLC_EGENERIC;
129
130     switch( i_query )
131     {
132     case PLAYLIST_STOP:
133         pl_priv(p_playlist)->request.i_status = PLAYLIST_STOPPED;
134         pl_priv(p_playlist)->request.b_request = true;
135         pl_priv(p_playlist)->request.p_item = NULL;
136         break;
137
138     // Node can be null, it will keep the same. Use with care ...
139     // Item null = take the first child of node
140     case PLAYLIST_VIEWPLAY:
141         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
142         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
143         if ( p_node == NULL )
144         {
145             p_node = get_current_status_node( p_playlist );
146             assert( p_node );
147         }
148         pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
149         pl_priv(p_playlist)->request.i_skip = 0;
150         pl_priv(p_playlist)->request.b_request = true;
151         pl_priv(p_playlist)->request.p_node = p_node;
152         pl_priv(p_playlist)->request.p_item = p_item;
153         if( p_item && var_GetBool( p_playlist, "random" ) )
154             pl_priv(p_playlist)->b_reset_currently_playing = true;
155         break;
156
157     case PLAYLIST_PLAY:
158         if( pl_priv(p_playlist)->p_input )
159         {
160             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
161             break;
162         }
163         else
164         {
165             pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
166             pl_priv(p_playlist)->request.b_request = true;
167             pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
168             pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
169             pl_priv(p_playlist)->request.i_skip = 0;
170         }
171         break;
172
173     case PLAYLIST_PAUSE:
174         if( !pl_priv(p_playlist)->p_input )
175         {    /* FIXME: is this really useful without input? */
176              pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
177              break;
178         }
179
180         if( var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
181         {
182             pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
183             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
184         }
185         else
186         {
187             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
188             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
189         }
190         break;
191
192     case PLAYLIST_SKIP:
193         pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
194         pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
195         pl_priv(p_playlist)->request.i_skip = (int) va_arg( args, int );
196         /* if already running, keep running */
197         if( pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED )
198             pl_priv(p_playlist)->request.i_status = pl_priv(p_playlist)->status.i_status;
199         pl_priv(p_playlist)->request.b_request = true;
200         break;
201
202     default:
203         msg_Err( p_playlist, "unknown playlist query" );
204         return VLC_EBADVAR;
205     }
206     vlc_cond_signal( &pl_priv(p_playlist)->signal );
207
208     return VLC_SUCCESS;
209 }
210
211 /*****************************************************************************
212  * Preparse control
213  *****************************************************************************/
214 /** Enqueue an item for preparsing */
215 int playlist_PreparseEnqueue( playlist_t *p_playlist,
216                               input_item_t *p_item, bool b_locked )
217 {
218     playlist_private_t *p_sys = pl_priv(p_playlist);
219
220     PL_LOCK_IF( !b_locked );
221     if( p_sys->p_preparser )
222         playlist_preparser_Push( p_sys->p_preparser, p_item );
223     PL_UNLOCK_IF( !b_locked );
224
225     return VLC_SUCCESS;
226 }
227
228 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
229                                input_item_t *p_item, bool b_locked )
230 {
231     playlist_private_t *p_sys = pl_priv(p_playlist);
232
233     PL_LOCK_IF( !b_locked );
234     if( p_sys->p_fetcher )
235         playlist_fetcher_Push( p_sys->p_fetcher, p_item );
236     PL_UNLOCK_IF( !b_locked );
237
238     return VLC_SUCCESS;
239 }
240