engine.c 18.3 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 52 53 54
    PL_LOCK;

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

    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;
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

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_CreateGetBool( p_playlist, "playlist-tree" );
102

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

105
    pl_priv(p_playlist)->b_auto_preparse =
ivoire's avatar
ivoire committed
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;
}

ivoire's avatar
ivoire committed
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 181
    if( p_sys->p_preparser )
        playlist_preparser_Delete( p_sys->p_preparser );
182

183 184
    if( p_sys->p_fetcher )
        playlist_fetcher_Delete( p_sys->p_fetcher );
185

186
    msg_Dbg( p_this, "Destroyed" );
187 188
}

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

203
    pl_priv(p_playlist)->b_cant_sleep = false;
204 205
}

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

213 214 215 216 217 218 219
    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 )
220
        pl_priv(p_playlist)->gc_date = mdate();
221 222 223 224

    vlc_object_signal_unlocked( p_playlist );

    PL_UNLOCK;
225
    return VLC_SUCCESS;
226 227
}

228 229 230
/* Internals */
void playlist_release_current_input( playlist_t * p_playlist )
{
231
    PL_ASSERT_LOCKED;
232

233
    if( !pl_priv(p_playlist)->p_input ) return;
234

235
    input_thread_t * p_input = pl_priv(p_playlist)->p_input;
236

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

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

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

    playlist_release_current_input( p_playlist );

    if( p_input )
    {
257
        vlc_object_hold( p_input );
258
        pl_priv(p_playlist)->p_input = p_input;
259 260

        var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
261 262 263
    }
}

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

276 277 278 279
/**
 * @}
 */

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

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

playlist_item_t * get_current_status_node( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

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

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

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

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

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

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

    PL_ASSERT_LOCKED;
340

341
    if( pl_priv(p_playlist)->b_reset_currently_playing &&
342
        mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
343
    {
ivoire's avatar
ivoire committed
344
        ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
345
                               get_current_status_item( p_playlist ) );
346
        pl_priv(p_playlist)->last_rebuild_date = mdate();
347
    }
348

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

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

zorglub's avatar
zorglub committed
366
            PL_DEBUG( "dead input" );
367

368
            p_input = pl_priv(p_playlist)->p_input;
369

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

            /* Destroy input */
375
            playlist_release_current_input( p_playlist );
376

377
            pl_priv(p_playlist)->gc_date = mdate();
378
            pl_priv(p_playlist)->b_cant_sleep = true;
379

ivoire's avatar
ivoire committed
380
            i_activity= var_GetInteger( p_playlist, "activity" );
381 382
            var_SetInteger( p_playlist, "activity", i_activity -
                            DEFAULT_INPUT_ACTIVITY );
383

384
            goto check_input;
385 386
        }
        /* This input is dying, let it do */
387
        else if( pl_priv(p_playlist)->p_input->b_die )
388
        {
zorglub's avatar
zorglub committed
389
            PL_DEBUG( "dying input" );
390
            PL_UNLOCK;
391
            msleep( INTF_IDLE_SLEEP );
392
            PL_LOCK;
393
            goto check_input;
394 395
        }
        /* This input has finished, ask it to die ! */
396 397
        else if( pl_priv(p_playlist)->p_input->b_error
                  || pl_priv(p_playlist)->p_input->b_eof )
398
        {
zorglub's avatar
zorglub committed
399
            PL_DEBUG( "finished input" );
400
            input_StopThread( pl_priv(p_playlist)->p_input );
401 402
            /* No need to wait here, we'll wait in the p_input->b_die case */
            goto check_input;
403
        }
404
        else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
405
        {
406
            ObjectGarbageCollector( p_playlist, false );
407 408 409 410 411 412 413 414 415
        }
    }
    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
ivoire's avatar
ivoire committed
416
        */
417 418
        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
419
        if( i_status != PLAYLIST_STOPPED )
ivoire's avatar
ivoire committed
420 421 422
        {
            msg_Dbg( p_playlist, "starting new item" );
            p_item = playlist_NextItem( p_playlist );
423

ivoire's avatar
ivoire committed
424 425
            if( p_item == NULL )
            {
426
                msg_Dbg( p_playlist, "nothing to play" );
427
                pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
428

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

445
            pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
446

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

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

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

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

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

        msleep( INTF_IDLE_SLEEP );
    }

503
#ifdef ENABLE_SOUT
504
    /* close the remaining sout-keep (if there was no input atm) */
505
    sout_instance_t *p_sout = pl_priv(p_playlist)->p_sout;
506 507
    if (p_sout)
        sout_DeleteInstance( p_sout );
508
#endif
509

510 511 512 513 514 515 516
    /* Core should have terminated all SDs before the playlist */
    /* TODO: It fails to do so when not playing anything -- Courmisch */
    playlist_ServicesDiscoveryKillAll( p_playlist );
    playlist_MLDump( p_playlist );

    PL_LOCK;

517 518 519 520 521
    /* Release the current node */
    set_current_status_node( p_playlist, NULL );

    /* Release the current item */
    set_current_status_item( p_playlist, NULL );
522

523 524
    FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
        free( p_del->pp_children );
Rafaël Carré's avatar
Rafaël Carré committed
525
        vlc_gc_decref( p_del->p_input );
526 527 528
        free( p_del );
    FOREACH_END();
    ARRAY_RESET( p_playlist->all_items );
529
    FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
530 531 532 533
        free( p_del->pp_children );
        vlc_gc_decref( p_del->p_input );
        free( p_del );
    FOREACH_END();
534
    ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
535 536 537 538 539

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

    PL_UNLOCK;
540 541 542 543 544 545 546
}

static void VariablesInit( playlist_t *p_playlist )
{
    vlc_value_t val;
    /* These variables control updates */
    var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
547
    val.b_bool = true;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    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 */
568 569 570 571 572
    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
573 574

    var_AddCallback( p_playlist, "random", RandomCallback, NULL );
575
}
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

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;
}