engine.c 17.1 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>
Clément Stenac's avatar
Clément Stenac committed
31 32 33
#include <vlc_sout.h>
#include <vlc_playlist.h>
#include <vlc_interface.h>
34
#include "playlist_internal.h"
Clément Stenac's avatar
Clément Stenac 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

Clément Stenac's avatar
Clément Stenac 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 52 53 54
    PL_LOCK;

    pl_priv(p_playlist)->b_reset_currently_playing = true;
    vlc_object_signal_unlocked( p_playlist );

    PL_UNLOCK;
Clément Stenac's avatar
Clément Stenac 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;
79
    TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
80

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

    VariablesInit( p_playlist );

    /* Initialise data structures */
86
    pl_priv(p_playlist)->i_last_playlist_id = 0;
87
    pl_priv(p_playlist)->p_input = NULL;
88

89
    pl_priv(p_playlist)->gc_date = 0;
90
    pl_priv(p_playlist)->b_cant_sleep = false;
91

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

Clément Stenac's avatar
Clément Stenac 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;
Clément Stenac's avatar
Clément Stenac committed
100

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

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

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

108
    PL_LOCK; /* playlist_NodeCreate will check for it */
109
    p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
110
                                    0, NULL );
111
    p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
112
                                    0, p_playlist->p_root_category->p_input );
113
    PL_UNLOCK;
114

115 116 117
    if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
        return NULL;

118
    /* Create playlist and media library */
119
    PL_LOCK; /* playlist_NodesPairCreate will check for it */
120 121
    playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
                            &p_playlist->p_local_category,
122
                            &p_playlist->p_local_onelevel, false );
123
    PL_UNLOCK;
124

125 126 127
    p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
    p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;

128 129 130 131 132
    if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
        !p_playlist->p_local_category->p_input ||
        !p_playlist->p_local_onelevel->p_input )
        return NULL;

133 134
    if( config_GetInt( p_playlist, "media-library") )
    {
135
        PL_LOCK; /* playlist_NodesPairCreate will check for it */
136 137
        playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
                            &p_playlist->p_ml_category,
138
                            &p_playlist->p_ml_onelevel, false );
139
        PL_UNLOCK;
140 141 142 143

        if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
            return NULL;

144 145 146 147 148 149 150
        p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
        p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
    }
    else
    {
        p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
    }
151 152

    /* Initial status */
153 154 155 156
    pl_priv(p_playlist)->status.p_item = NULL;
    pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
    pl_priv(p_playlist)->request.b_request = false;
    pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
157

158
    pl_priv(p_playlist)->b_auto_preparse = false;
159
    playlist_MLLoad( p_playlist );
160
    pl_priv(p_playlist)->b_auto_preparse = true;
161

162 163
    vlc_object_set_destructor( p_playlist, playlist_Destructor );

164 165 166
    return p_playlist;
}

167 168 169 170 171 172 173
/**
 * Destroy playlist
 *
 * Destroy a playlist structure.
 * \param p_playlist the playlist object
 * \return nothing
 */
174 175 176

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

180
    assert( !p_sys->p_sout );
181 182
    assert( !p_sys->p_preparser );
    assert( !p_sys->p_fetcher );
183

184
    msg_Err( p_this, "Destroyed" );
185 186
}

187
/* Destroy remaining objects */
188
static void ObjectGarbageCollector( playlist_t *p_playlist, bool b_force )
189
{
190
    if( !b_force )
191
    {
192
        if( mdate() - pl_priv(p_playlist)->gc_date < 1000000 )
193
        {
194
           pl_priv(p_playlist)->b_cant_sleep = true;
195 196
            return;
        }
197
        else if( pl_priv(p_playlist)->gc_date == 0 )
198
            return;
199
    }
200

201
    pl_priv(p_playlist)->b_cant_sleep = false;
202 203
}

204
/* Input Callback */
205 206
static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
207
{
208 209
    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
    playlist_t *p_playlist = p_data;
210

211 212 213 214 215 216 217
    if( newval.i_int != INPUT_EVENT_STATE &&
        newval.i_int != INPUT_EVENT_ES )
        return VLC_SUCCESS;

    PL_LOCK;

    if( newval.i_int == INPUT_EVENT_ES )
218
        pl_priv(p_playlist)->gc_date = mdate();
219 220 221 222

    vlc_object_signal_unlocked( p_playlist );

    PL_UNLOCK;
223
    return VLC_SUCCESS;
224 225
}

226 227 228
/* Internals */
void playlist_release_current_input( playlist_t * p_playlist )
{
229
    PL_ASSERT_LOCKED;
230

231
    if( !pl_priv(p_playlist)->p_input ) return;
232

233
    input_thread_t * p_input = pl_priv(p_playlist)->p_input;
234

235
    var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
236
    pl_priv(p_playlist)->p_input = NULL;
237 238 239 240

    /* Release the playlist lock, because we may get stuck
     * in vlc_object_release() for some time. */
    PL_UNLOCK;
241
    vlc_thread_join( p_input );
242 243 244 245 246 247 248
    vlc_object_release( p_input );
    PL_LOCK;
}

void playlist_set_current_input(
    playlist_t * p_playlist, input_thread_t * p_input )
{
249
    PL_ASSERT_LOCKED;
250 251 252 253 254

    playlist_release_current_input( p_playlist );

    if( p_input )
    {
255
        vlc_object_hold( p_input );
256
        pl_priv(p_playlist)->p_input = p_input;
257 258

        var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
259 260 261
    }
}

262 263 264 265 266 267
/** Get current playing input.
 */
input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
{
    input_thread_t * p_input;
    PL_LOCK;
268
    p_input = pl_priv(p_playlist)->p_input;
269
    if( p_input ) vlc_object_hold( p_input );
270 271 272 273
    PL_UNLOCK;
    return p_input;
}

274 275 276 277
/**
 * @}
 */

278 279 280 281 282 283
/** Accessor for status item and status nodes.
 */
playlist_item_t * get_current_status_item( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

284
    return pl_priv(p_playlist)->status.p_item;
285 286 287 288 289 290
}

playlist_item_t * get_current_status_node( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

291
    return pl_priv(p_playlist)->status.p_node;
292 293 294 295 296 297 298
}

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

299 300 301
    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 )
302
    {
303
        /* It's unsafe given current design to delete a playlist item :(
304
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
305
    }
306
    pl_priv(p_playlist)->status.p_item = p_item;
307 308 309 310 311 312 313
}

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

314 315 316
    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 )
317
    {
318
        /* It's unsafe given current design to delete a playlist item :(
319
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
320
    }
321
    pl_priv(p_playlist)->status.p_node = p_node;
322 323
}

324 325 326
/**
 * Main loop
 *
Laurent Aimar's avatar
Laurent Aimar committed
327 328
 * Main loop for the playlist. It should be entered with the
 * playlist lock (otherwise input event may be lost)
329 330 331
 * \param p_playlist the playlist object
 * \return nothing
 */
332 333 334
void playlist_MainLoop( playlist_t *p_playlist )
{
    playlist_item_t *p_item = NULL;
335
    bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
Laurent Aimar's avatar
Laurent Aimar committed
336 337

    PL_ASSERT_LOCKED;
338

339
    if( pl_priv(p_playlist)->b_reset_currently_playing &&
340
        mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
341
    {
342
        ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
343
                               get_current_status_item( p_playlist ) );
344
        pl_priv(p_playlist)->last_rebuild_date = mdate();
345
    }
346

347
check_input:
348
    /* If there is an input, check that it doesn't need to die. */
349
    if( pl_priv(p_playlist)->p_input )
350
    {
351
        if( pl_priv(p_playlist)->request.b_request && !pl_priv(p_playlist)->p_input->b_die )
352 353
        {
            PL_DEBUG( "incoming request - stopping current input" );
354
            input_StopThread( pl_priv(p_playlist)->p_input );
355 356
        }

357
        /* This input is dead. Remove it ! */
358
        if( pl_priv(p_playlist)->p_input->b_dead )
359 360 361
        {
            int i_activity;
            input_thread_t *p_input;
362
            sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
363

364
            PL_DEBUG( "dead input" );
365

366
            p_input = pl_priv(p_playlist)->p_input;
367

368 369 370
            assert( *pp_sout == NULL );
            if( var_CreateGetBool( p_input, "sout-keep" ) )
                *pp_sout = input_DetachSout( p_input );
371 372

            /* Destroy input */
373
            playlist_release_current_input( p_playlist );
374

375
            pl_priv(p_playlist)->gc_date = mdate();
376
            pl_priv(p_playlist)->b_cant_sleep = true;
377

378
            i_activity= var_GetInteger( p_playlist, "activity" );
379 380
            var_SetInteger( p_playlist, "activity", i_activity -
                            DEFAULT_INPUT_ACTIVITY );
381

382
            goto check_input;
383 384
        }
        /* This input is dying, let it do */
385
        else if( pl_priv(p_playlist)->p_input->b_die )
386
        {
387
            PL_DEBUG( "dying input" );
388
            PL_UNLOCK;
389
            msleep( INTF_IDLE_SLEEP );
390
            PL_LOCK;
391
            goto check_input;
392 393
        }
        /* This input has finished, ask it to die ! */
394 395
        else if( pl_priv(p_playlist)->p_input->b_error
                  || pl_priv(p_playlist)->p_input->b_eof )
396
        {
397
            PL_DEBUG( "finished input" );
398
            input_StopThread( pl_priv(p_playlist)->p_input );
399 400
            /* No need to wait here, we'll wait in the p_input->b_die case */
            goto check_input;
401
        }
402
        else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
403
        {
404
            ObjectGarbageCollector( p_playlist, false );
405 406 407 408 409 410 411 412 413
        }
    }
    else
    {
        /* No input. Several cases
         *  - No request, running status -> start new item
         *  - No request, stopped status -> collect garbage
         *  - Request, running requested -> start new item
         *  - Request, stopped requested -> collect garbage
414
        */
415 416
        int i_status = pl_priv(p_playlist)->request.b_request ?
            pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
Rafaël Carré's avatar
Rafaël Carré committed
417
        if( i_status != PLAYLIST_STOPPED )
418 419 420
        {
            msg_Dbg( p_playlist, "starting new item" );
            p_item = playlist_NextItem( p_playlist );
421

422 423
            if( p_item == NULL )
            {
424
                msg_Dbg( p_playlist, "nothing to play" );
425
                pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
426

427
                if( b_playexit == true )
428 429
                {
                    msg_Info( p_playlist, "end of playlist, exiting" );
430
                    vlc_object_kill( p_playlist->p_libvlc );
431
                }
432
                ObjectGarbageCollector( p_playlist, true );
433
                return;
Rémi Duraffort's avatar
Rémi Duraffort committed
434 435
            }
            playlist_PlayItem( p_playlist, p_item );
Laurent Aimar's avatar
Laurent Aimar committed
436
            /* playlist_PlayItem loose input event, we need to recheck */
Laurent Aimar's avatar
Laurent Aimar committed
437
            goto check_input;
Rémi Duraffort's avatar
Rémi Duraffort committed
438 439 440
        }
        else
        {
441
            const bool b_gc_forced = pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED;
442

443
            pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
444

445
            /* Collect garbage */
446
            ObjectGarbageCollector( p_playlist, b_gc_forced );
447
        }
448 449 450
    }
}

451 452 453 454 455 456 457
/**
 * Last loop
 *
 * The playlist is dying so do the last loop
 * \param p_playlist the playlist object
 * \return nothing
*/
458 459 460 461 462
void playlist_LastLoop( playlist_t *p_playlist )
{
    /* If there is an input, kill it */
    while( 1 )
    {
463
        PL_LOCK;
464
        if( pl_priv(p_playlist)->p_input == NULL )
465
        {
466
            PL_UNLOCK;
467 468 469
            break;
        }

470
        if( pl_priv(p_playlist)->p_input->b_dead )
471
        {
472 473
            /* remove input */
            playlist_release_current_input( p_playlist );
474

475 476 477
            /* sout-keep: no need to anything here.
             * The last input will destroy its sout, if any, by itself */

478
            PL_UNLOCK;
479 480
            continue;
        }
481
        else if( pl_priv(p_playlist)->p_input->b_die )
482 483 484 485
        {
            /* This input is dying, leave it alone */
            ;
        }
486
        else if( pl_priv(p_playlist)->p_input->b_error || pl_priv(p_playlist)->p_input->b_eof )
487
        {
488
            input_StopThread( pl_priv(p_playlist)->p_input );
489
            PL_UNLOCK;
490 491 492 493
            continue;
        }
        else
        {
494
            pl_priv(p_playlist)->p_input->b_eof = 1;
495
        }
496
        PL_UNLOCK;
497 498 499 500 501 502 503 504 505 506

        msleep( INTF_IDLE_SLEEP );
    }
}

static void VariablesInit( playlist_t *p_playlist )
{
    vlc_value_t val;
    /* These variables control updates */
    var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
507
    val.b_bool = true;
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    var_Set( p_playlist, "intf-change", val );

    var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
    val.i_int = -1;
    var_Set( p_playlist, "item-change", val );

    var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
    val.i_int = -1;
    var_Set( p_playlist, "item-deleted", val );

    var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );

    var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
    val.i_int = -1;
    var_Set( p_playlist, "playlist-current", val );

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

    /* Variables to control playback */
528 529 530 531 532
    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 );
Clément Stenac's avatar
Clément Stenac committed
533 534

    var_AddCallback( p_playlist, "random", RandomCallback, NULL );
535 536 537

    /* */
    var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
538
}
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

int playlist_CurrentId( playlist_t * p_playlist )
{
    return pl_priv(p_playlist)->status.p_item->i_id;
}

bool playlist_IsPlaying( playlist_t * p_playlist )
{
    return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
            !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
}

playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
{
    return pl_priv(p_playlist)->status.p_item;
}

int playlist_Status( playlist_t * p_playlist )
{
    return pl_priv(p_playlist)->status.i_status;
}