engine.c 9.79 KB
Newer Older
1 2 3
/*****************************************************************************
 * engine.c : Run the playlist and handle its control
 *****************************************************************************
4
 * Copyright (C) 1999-2008 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *****************************************************************************/
23

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <stddef.h>
29
#include <assert.h>
30
#include <vlc_common.h>
zorglub's avatar
zorglub committed
31 32 33
#include <vlc_sout.h>
#include <vlc_playlist.h>
#include <vlc_interface.h>
34
#include "playlist_internal.h"
zorglub's avatar
zorglub committed
35
#include "stream_output/stream_output.h" /* sout_DeleteInstance */
36 37 38 39 40

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void VariablesInit( playlist_t *p_playlist );
41
static void playlist_Destructor( vlc_object_t * p_this );
42

zorglub's avatar
zorglub committed
43 44 45
static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
                           vlc_value_t oldval, vlc_value_t newval, void *a )
{
46
    (void)psz_cmd; (void)oldval; (void)newval; (void)a;
47
    playlist_t *p_playlist = (playlist_t*)p_this;
48

49 50 51
    PL_LOCK;

    pl_priv(p_playlist)->b_reset_currently_playing = true;
52
    vlc_cond_signal( &pl_priv(p_playlist)->signal );
53 54

    PL_UNLOCK;
zorglub's avatar
zorglub committed
55 56 57
    return VLC_SUCCESS;
}

58 59 60 61 62 63 64 65 66
/**
 * Create playlist
 *
 * Create a playlist structure.
 * \param p_parent the vlc object that is to be the parent of this playlist
 * \return a pointer to the created playlist, or NULL on error
 */
playlist_t * playlist_Create( vlc_object_t *p_parent )
{
67
    static const char playlist_name[] = "playlist";
68
    playlist_t *p_playlist;
69
    playlist_private_t *p;
70 71

    /* Allocate structure */
72 73 74
    p = vlc_custom_create( p_parent, sizeof( *p ),
                           VLC_OBJECT_GENERIC, playlist_name );
    if( !p )
75
        return NULL;
76

77 78
    assert( offsetof( playlist_private_t, public_data ) == 0 );
    p_playlist = &p->public_data;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
79
    vlc_object_attach( p_playlist, p_parent );
80
    TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
81

82
    libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
83 84

    VariablesInit( p_playlist );
85
    vlc_mutex_init( &p->lock );
86
    vlc_cond_init( &p->signal );
87 88

    /* Initialise data structures */
89
    pl_priv(p_playlist)->i_last_playlist_id = 0;
90
    pl_priv(p_playlist)->p_input = NULL;
91

zorglub's avatar
zorglub committed
92 93
    ARRAY_INIT( p_playlist->items );
    ARRAY_INIT( p_playlist->all_items );
94
    ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
zorglub's avatar
zorglub committed
95
    ARRAY_INIT( p_playlist->current );
96

zorglub's avatar
zorglub committed
97
    p_playlist->i_current_index = 0;
98
    pl_priv(p_playlist)->b_reset_currently_playing = true;
99
    pl_priv(p_playlist)->last_rebuild_date = 0;
zorglub's avatar
zorglub committed
100

101
    pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
102

103
    pl_priv(p_playlist)->b_doing_ml = false;
104

105 106
    pl_priv(p_playlist)->b_auto_preparse =
        var_InheritBool( p_parent, "auto-preparse" );
107

jpd's avatar
jpd committed
108 109 110
    /* Create the root node */
    PL_LOCK;
    p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
111
                                    0, NULL );
112
    PL_UNLOCK;
jpd's avatar
jpd committed
113
    if( !p_playlist->p_root ) return NULL;
114

jpd's avatar
jpd committed
115 116 117 118 119
    /* Create currently playing items node */
    PL_LOCK;
    p_playlist->p_playing = playlist_NodeCreate(
        p_playlist, _( "Playlist" ), p_playlist->p_root,
        PLAYLIST_RO_FLAG, NULL );
120

121
    PL_UNLOCK;
122

jpd's avatar
jpd committed
123
    if( !p_playlist->p_playing ) return NULL;
124

jpd's avatar
jpd committed
125
    /* Create media library node */
126 127
    const bool b_ml = var_InheritBool( p_parent, "media-library");
    if( b_ml )
128
    {
jpd's avatar
jpd committed
129 130 131 132
        PL_LOCK;
        p_playlist->p_media_library = playlist_NodeCreate(
            p_playlist, _( "Media Library" ), p_playlist->p_root,
            PLAYLIST_RO_FLAG, NULL );
133
        PL_UNLOCK;
134

jpd's avatar
jpd committed
135
        if(!p_playlist->p_media_library ) return NULL;
136 137 138
    }
    else
    {
jpd's avatar
jpd committed
139
        p_playlist->p_media_library = NULL;
140
    }
141

jpd's avatar
jpd committed
142 143 144 145 146 147 148
    p_playlist->p_root_category = p_playlist->p_root;
    p_playlist->p_root_onelevel = p_playlist->p_root;
    p_playlist->p_local_category = p_playlist->p_playing;
    p_playlist->p_local_onelevel = p_playlist->p_playing;
    p_playlist->p_ml_category = p_playlist->p_media_library;
    p_playlist->p_ml_onelevel = p_playlist->p_media_library;;

149
    /* Initial status */
150
    pl_priv(p_playlist)->status.p_item = NULL;
jpd's avatar
jpd committed
151
    pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
152 153
    pl_priv(p_playlist)->request.b_request = false;
    pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
154

155 156 157 158 159 160 161
    if(b_ml)
    {
        const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse;
        pl_priv(p_playlist)->b_auto_preparse = false;
        playlist_MLLoad( p_playlist );
        pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse;
    }
162

163 164
    vlc_object_set_destructor( p_playlist, playlist_Destructor );

165 166 167
    return p_playlist;
}

ivoire's avatar
ivoire committed
168 169 170 171 172 173 174
/**
 * Destroy playlist
 *
 * Destroy a playlist structure.
 * \param p_playlist the playlist object
 * \return nothing
 */
175 176 177

static void playlist_Destructor( vlc_object_t * p_this )
{
178 179
    playlist_t *p_playlist = (playlist_t *)p_this;
    playlist_private_t *p_sys = pl_priv(p_playlist);
180

Laurent Aimar's avatar
Laurent Aimar committed
181
    assert( !p_sys->p_input );
182
    assert( !p_sys->p_input_resource );
183 184
    assert( !p_sys->p_preparser );
    assert( !p_sys->p_fetcher );
185

186
    vlc_cond_destroy( &p_sys->signal );
187
    vlc_mutex_destroy( &p_sys->lock );
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    /* Remove all remaining items */
    FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
        free( p_del->pp_children );
        vlc_gc_decref( p_del->p_input );
        free( p_del );
    FOREACH_END();
    ARRAY_RESET( p_playlist->all_items );
    FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
        free( p_del->pp_children );
        vlc_gc_decref( p_del->p_input );
        free( p_del );
    FOREACH_END();
    ARRAY_RESET( p_sys->items_to_delete );

    ARRAY_RESET( p_playlist->items );
    ARRAY_RESET( p_playlist->current );

Laurent Aimar's avatar
Laurent Aimar committed
206
    msg_Dbg( p_this, "Destroyed" );
207 208
}

209 210 211 212 213 214
/** Get current playing input.
 */
input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
{
    input_thread_t * p_input;
    PL_LOCK;
215
    p_input = pl_priv(p_playlist)->p_input;
216
    if( p_input ) vlc_object_hold( p_input );
217 218 219 220
    PL_UNLOCK;
    return p_input;
}

221 222 223 224
/**
 * @}
 */

225 226 227 228 229 230
/** Accessor for status item and status nodes.
 */
playlist_item_t * get_current_status_item( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

231
    return pl_priv(p_playlist)->status.p_item;
232 233 234 235 236 237
}

playlist_item_t * get_current_status_node( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

238
    return pl_priv(p_playlist)->status.p_node;
239 240 241 242 243 244 245
}

void set_current_status_item( playlist_t * p_playlist,
    playlist_item_t * p_item )
{
    PL_ASSERT_LOCKED;

246 247 248
    if( pl_priv(p_playlist)->status.p_item &&
        pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
        pl_priv(p_playlist)->status.p_item != p_item )
249
    {
250
        /* It's unsafe given current design to delete a playlist item :(
251
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
252
    }
253
    pl_priv(p_playlist)->status.p_item = p_item;
254 255 256 257 258 259 260
}

void set_current_status_node( playlist_t * p_playlist,
    playlist_item_t * p_node )
{
    PL_ASSERT_LOCKED;

261 262 263
    if( pl_priv(p_playlist)->status.p_node &&
        pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
        pl_priv(p_playlist)->status.p_node != p_node )
264
    {
265
        /* It's unsafe given current design to delete a playlist item :(
266
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
267
    }
268
    pl_priv(p_playlist)->status.p_node = p_node;
269 270
}

271 272 273 274
static void VariablesInit( playlist_t *p_playlist )
{
    /* These variables control updates */
    var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
Laurent Aimar's avatar
Laurent Aimar committed
275
    var_SetBool( p_playlist, "intf-change", true );
276

277
    var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
278
    var_Create( p_playlist, "leaf-to-parent", VLC_VAR_ADDRESS );
279

280 281
    var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
    var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
282

283
    var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
284

285
    var_Create( p_playlist, "item-current", VLC_VAR_ADDRESS );
286
    var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
287 288 289 290 291

    var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
    var_SetInteger( p_playlist, "activity", 0 );

    /* Variables to control playback */
292 293 294 295 296
    var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
zorglub's avatar
zorglub committed
297 298

    var_AddCallback( p_playlist, "random", RandomCallback, NULL );
299 300 301

    /* */
    var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
302
}
303 304 305

playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
{
306 307
    PL_ASSERT_LOCKED;

308 309 310 311 312
    return pl_priv(p_playlist)->status.p_item;
}

int playlist_Status( playlist_t * p_playlist )
{
313 314
    PL_ASSERT_LOCKED;

315 316
    return pl_priv(p_playlist)->status.i_status;
}
317