control.c 7.15 KB
Newer Older
1
/*****************************************************************************
zorglub's avatar
zorglub committed
2
 * control.c : Handle control of the playlist & running through it
3
4
 *****************************************************************************
 * Copyright (C) 1999-2004 the VideoLAN team
Jean-Paul Saman's avatar
Jean-Paul Saman committed
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
24
25
26
27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
29
#include "vlc_playlist.h"
30
#include "playlist_internal.h"
31
#include <assert.h>
32
33
34
35

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
36
static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
37
38
39
40
41

/*****************************************************************************
 * Playlist control
 *****************************************************************************/

dionoea's avatar
dionoea committed
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
dionoea's avatar
dionoea committed
50
    using pl_Hold. use vlc_object_hold in this case */ );
51

52
    if (pl)
53
        vlc_object_hold (pl);
54
55
56
57
58
    return pl;
}

void __pl_Release( vlc_object_t *p_this )
{
59
    playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
60
    assert( pl != NULL );
61

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
63
64
    /* The rule is that pl_Release() should act on
       the same object than pl_Hold() */
    assert( VLC_OBJECT(pl) != p_this);
65

66
67
68
    vlc_object_release( pl );
}

69
70
void playlist_Lock( playlist_t *pl )
{
71
    vlc_mutex_lock( &pl_priv(pl)->lock );
72
73
74
75
}

void playlist_Unlock( playlist_t *pl )
{
76
    vlc_mutex_unlock( &pl_priv(pl)->lock );
77
78
79
80
}

void playlist_AssertLocked( playlist_t *pl )
{
81
    vlc_assert_locked( &pl_priv(pl)->lock );
82
83
}

84
int playlist_Control( playlist_t * p_playlist, int i_query,
85
                      bool b_locked, ... )
86
87
88
{
    va_list args;
    int i_result;
89
    PL_LOCK_IF( !b_locked );
90
    va_start( args, b_locked );
91
92
    i_result = PlaylistVAControl( p_playlist, i_query, args );
    va_end( args );
93
    PL_UNLOCK_IF( !b_locked );
94
95
96
97

    return i_result;
}

98
static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
99
100
101
{
    playlist_item_t *p_item, *p_node;

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
102
103
    PL_ASSERT_LOCKED;

104
105
106
    if( !vlc_object_alive( p_playlist ) )
        return VLC_EGENERIC;

107
    if( playlist_IsEmpty( p_playlist ) )
108
109
110
111
112
        return VLC_EGENERIC;

    switch( i_query )
    {
    case PLAYLIST_STOP:
113
114
115
        pl_priv(p_playlist)->request.i_status = PLAYLIST_STOPPED;
        pl_priv(p_playlist)->request.b_request = true;
        pl_priv(p_playlist)->request.p_item = NULL;
116
117
        break;

118
119
    // Node can be null, it will keep the same. Use with care ...
    // Item null = take the first child of node
120
121
122
123
124
    case PLAYLIST_VIEWPLAY:
        p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
        p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
        if ( p_node == NULL )
        {
125
            p_node = get_current_status_node( p_playlist );
126
            assert( p_node );
127
        }
128
129
130
131
132
        pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
        pl_priv(p_playlist)->request.i_skip = 0;
        pl_priv(p_playlist)->request.b_request = true;
        pl_priv(p_playlist)->request.p_node = p_node;
        pl_priv(p_playlist)->request.p_item = p_item;
zorglub's avatar
zorglub committed
133
        if( p_item && var_GetBool( p_playlist, "random" ) )
134
            pl_priv(p_playlist)->b_reset_currently_playing = true;
135
136
137
        break;

    case PLAYLIST_PLAY:
138
        if( pl_priv(p_playlist)->p_input )
139
        {
Laurent Aimar's avatar
Laurent Aimar committed
140
            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
141
142
            break;
        }
143
144
        else
        {
145
146
147
148
149
            pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
            pl_priv(p_playlist)->request.b_request = true;
            pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
            pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
            pl_priv(p_playlist)->request.i_skip = 0;
150
        }
151
152
153
        break;

    case PLAYLIST_PAUSE:
Laurent Aimar's avatar
Laurent Aimar committed
154
155
        if( pl_priv(p_playlist)->p_input &&
            var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
156
        {
157
158
            pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
            if( pl_priv(p_playlist)->p_input )
159
            {
Laurent Aimar's avatar
Laurent Aimar committed
160
                var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
161
162
163
164
            }
        }
        else
        {
165
166
            pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
            if( pl_priv(p_playlist)->p_input )
167
            {
Laurent Aimar's avatar
Laurent Aimar committed
168
                var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
169
170
171
172
173
            }
        }
        break;

    case PLAYLIST_SKIP:
174
175
176
        pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
        pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
        pl_priv(p_playlist)->request.i_skip = (int) va_arg( args, int );
zorglub's avatar
zorglub committed
177
        /* if already running, keep running */
178
179
180
        if( pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED )
            pl_priv(p_playlist)->request.i_status = pl_priv(p_playlist)->status.i_status;
        pl_priv(p_playlist)->request.b_request = true;
181
182
183
184
185
186
        break;

    default:
        msg_Err( p_playlist, "unknown playlist query" );
        return VLC_EBADVAR;
    }
187
    vlc_cond_signal( &pl_priv(p_playlist)->signal );
188
189
190
191
192
193
194
195
196

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Preparse control
 *****************************************************************************/
/** Enqueue an item for preparsing */
int playlist_PreparseEnqueue( playlist_t *p_playlist,
197
                              input_item_t *p_item, bool b_locked )
198
{
199
    playlist_private_t *p_sys = pl_priv(p_playlist);
200

201
    PL_LOCK_IF( !b_locked );
Laurent Aimar's avatar
Laurent Aimar committed
202
203
    if( p_sys->p_preparser )
        playlist_preparser_Push( p_sys->p_preparser, p_item );
204
    PL_UNLOCK_IF( !b_locked );
205

206
207
208
    return VLC_SUCCESS;
}

dionoea's avatar
dionoea committed
209
int playlist_AskForArtEnqueue( playlist_t *p_playlist,
210
                               input_item_t *p_item, bool b_locked )
dionoea's avatar
dionoea committed
211
{
212
    playlist_private_t *p_sys = pl_priv(p_playlist);
213

214
    PL_LOCK_IF( !b_locked );
Laurent Aimar's avatar
Laurent Aimar committed
215
216
    if( p_sys->p_fetcher )
        playlist_fetcher_Push( p_sys->p_fetcher, p_item );
217
    PL_UNLOCK_IF( !b_locked );
218

dionoea's avatar
dionoea committed
219
220
221
    return VLC_SUCCESS;
}