engine.c 24.9 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 47
    (void)psz_cmd; (void)oldval; (void)newval; (void)a;

48
    pl_priv((playlist_t*)p_this)->b_reset_currently_playing = true;
49
    playlist_Signal( ((playlist_t*)p_this) );
zorglub's avatar
zorglub committed
50 51 52
    return VLC_SUCCESS;
}

53 54 55 56 57 58 59 60 61
/**
 * 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 )
{
62
    static const char playlist_name[] = "playlist";
63
    playlist_t *p_playlist;
64
    playlist_private_t *p;
65 66

    /* Allocate structure */
67 68 69
    p = vlc_custom_create( p_parent, sizeof( *p ),
                           VLC_OBJECT_GENERIC, playlist_name );
    if( !p )
70
        return NULL;
71

72 73
    assert( offsetof( playlist_private_t, public_data ) == 0 );
    p_playlist = &p->public_data;
74
    TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
75

76
    libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
77 78 79 80

    VariablesInit( p_playlist );

    /* Initialise data structures */
81
    pl_priv(p_playlist)->i_last_playlist_id = 0;
82
    pl_priv(p_playlist)->p_input = NULL;
83

84
    pl_priv(p_playlist)->gc_date = 0;
85
    pl_priv(p_playlist)->b_cant_sleep = false;
86

zorglub's avatar
zorglub committed
87 88
    ARRAY_INIT( p_playlist->items );
    ARRAY_INIT( p_playlist->all_items );
89
    ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
zorglub's avatar
zorglub committed
90
    ARRAY_INIT( p_playlist->current );
91

zorglub's avatar
zorglub committed
92
    p_playlist->i_current_index = 0;
93
    pl_priv(p_playlist)->b_reset_currently_playing = true;
94
    pl_priv(p_playlist)->last_rebuild_date = 0;
zorglub's avatar
zorglub committed
95

96
    pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
97

98
    pl_priv(p_playlist)->b_doing_ml = false;
99

100
    pl_priv(p_playlist)->b_auto_preparse =
ivoire's avatar
ivoire committed
101
                        var_CreateGetBool( p_playlist, "auto-preparse" ) ;
102

103
    PL_LOCK; /* playlist_NodeCreate will check for it */
104
    p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
105
                                    0, NULL );
106
    p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
107
                                    0, p_playlist->p_root_category->p_input );
108
    PL_UNLOCK;
109

110 111 112
    if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
        return NULL;

113
    /* Create playlist and media library */
114
    PL_LOCK; /* playlist_NodesPairCreate will check for it */
115 116
    playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
                            &p_playlist->p_local_category,
117
                            &p_playlist->p_local_onelevel, false );
118
    PL_UNLOCK;
119

120 121 122
    p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
    p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;

123 124 125 126 127
    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;

128 129
    if( config_GetInt( p_playlist, "media-library") )
    {
130
        PL_LOCK; /* playlist_NodesPairCreate will check for it */
131 132
        playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
                            &p_playlist->p_ml_category,
133
                            &p_playlist->p_ml_onelevel, false );
134
        PL_UNLOCK;
135 136 137 138

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

139 140 141 142 143 144 145
        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;
    }
146 147

    /* Initial status */
148 149 150 151
    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;
152

153
    pl_priv(p_playlist)->b_auto_preparse = false;
154
    playlist_MLLoad( p_playlist );
155
    pl_priv(p_playlist)->b_auto_preparse = true;
156

157 158
    vlc_object_set_destructor( p_playlist, playlist_Destructor );

159 160 161
    return p_playlist;
}

ivoire's avatar
ivoire committed
162 163 164 165 166 167 168
/**
 * Destroy playlist
 *
 * Destroy a playlist structure.
 * \param p_playlist the playlist object
 * \return nothing
 */
169 170 171 172 173

static void playlist_Destructor( vlc_object_t * p_this )
{
    playlist_t * p_playlist = (playlist_t *)p_this;

174
    /* Destroy the item preparser */
175
    playlist_preparse_t *p_preparse = &pl_priv(p_playlist)->preparse;
176
    if (p_preparse->up)
177
    {
178 179
        vlc_cancel (p_preparse->thread);
        vlc_join (p_preparse->thread, NULL);
180
    }
181 182 183 184 185 186 187
    while (p_preparse->i_waiting > 0)
    {   /* Any left-over unparsed item? */
        vlc_gc_decref (p_preparse->pp_waiting[0]);
        REMOVE_ELEM (p_preparse->pp_waiting, p_preparse->i_waiting, 0);
    }
    vlc_cond_destroy (&p_preparse->wait);
    vlc_mutex_destroy (&p_preparse->lock);
188

189
    /* Destroy the item meta-infos fetcher */
190 191
    playlist_fetcher_t *p_fetcher = &pl_priv(p_playlist)->fetcher;
    if (p_fetcher->up)
192
    {
193 194 195 196 197 198 199
        vlc_cancel (p_fetcher->thread);
        vlc_join (p_fetcher->thread, NULL);
    }
    while (p_fetcher->i_waiting > 0)
    {   /* Any left-over unparsed item? */
        vlc_gc_decref (p_fetcher->pp_waiting[0]);
        REMOVE_ELEM (p_fetcher->pp_waiting, p_fetcher->i_waiting, 0);
200
    }
201 202 203
    vlc_cond_destroy (&p_fetcher->wait);
    vlc_mutex_destroy (&p_fetcher->lock);

204
    msg_Dbg( p_this, "Destroyed" );
205 206
}

207
/* Destroy remaining objects */
208
static void ObjectGarbageCollector( playlist_t *p_playlist, bool b_force )
209
{
210
    if( !b_force )
211
    {
212
        if( mdate() - pl_priv(p_playlist)->gc_date < 1000000 )
213
        {
214
           pl_priv(p_playlist)->b_cant_sleep = true;
215 216
            return;
        }
217
        else if( pl_priv(p_playlist)->gc_date == 0 )
218
            return;
219
    }
220

221
    pl_priv(p_playlist)->b_cant_sleep = false;
222 223
}

224
/* Input Callback */
225 226
static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
227
{
228 229
    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
    playlist_t *p_playlist = p_data;
230

231 232 233 234 235 236 237 238 239 240 241 242
    if( newval.i_int == INPUT_EVENT_STATE )
    {
        playlist_Signal( p_playlist );
    }
    else if( newval.i_int == INPUT_EVENT_ES )
    {
        PL_LOCK;
        pl_priv(p_playlist)->gc_date = mdate();
        vlc_object_signal_unlocked( p_playlist );
        PL_UNLOCK;
    }
    return VLC_SUCCESS;
243 244
}

245 246 247
/* Internals */
void playlist_release_current_input( playlist_t * p_playlist )
{
248
    PL_ASSERT_LOCKED;
249

250
    if( !pl_priv(p_playlist)->p_input ) return;
251

252
    input_thread_t * p_input = pl_priv(p_playlist)->p_input;
253

254
    var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
255
    pl_priv(p_playlist)->p_input = NULL;
256 257 258 259

    /* Release the playlist lock, because we may get stuck
     * in vlc_object_release() for some time. */
    PL_UNLOCK;
260
    vlc_thread_join( p_input );
261 262 263 264 265 266 267
    vlc_object_release( p_input );
    PL_LOCK;
}

void playlist_set_current_input(
    playlist_t * p_playlist, input_thread_t * p_input )
{
268
    PL_ASSERT_LOCKED;
269 270 271 272 273

    playlist_release_current_input( p_playlist );

    if( p_input )
    {
274
        vlc_object_hold( p_input );
275
        pl_priv(p_playlist)->p_input = p_input;
276 277

        var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
278 279 280
    }
}

281 282 283 284 285 286
/** Get current playing input.
 */
input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
{
    input_thread_t * p_input;
    PL_LOCK;
287
    p_input = pl_priv(p_playlist)->p_input;
288
    if( p_input ) vlc_object_hold( p_input );
289 290 291 292
    PL_UNLOCK;
    return p_input;
}

293 294 295 296
/**
 * @}
 */

297 298 299 300 301 302
/** Accessor for status item and status nodes.
 */
playlist_item_t * get_current_status_item( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

303
    return pl_priv(p_playlist)->status.p_item;
304 305 306 307 308 309
}

playlist_item_t * get_current_status_node( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

310
    return pl_priv(p_playlist)->status.p_node;
311 312 313 314 315 316 317
}

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

318 319 320
    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 )
321
    {
322
        /* It's unsafe given current design to delete a playlist item :(
323
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
324
    }
325
    pl_priv(p_playlist)->status.p_item = p_item;
326 327 328 329 330 331 332
}

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

333 334 335
    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 )
336
    {
337
        /* It's unsafe given current design to delete a playlist item :(
338
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
339
    }
340
    pl_priv(p_playlist)->status.p_node = p_node;
341 342
}

ivoire's avatar
ivoire committed
343 344 345
/**
 * Main loop
 *
Laurent Aimar's avatar
Laurent Aimar committed
346 347
 * Main loop for the playlist. It should be entered with the
 * playlist lock (otherwise input event may be lost)
ivoire's avatar
ivoire committed
348 349 350
 * \param p_playlist the playlist object
 * \return nothing
 */
351 352 353
void playlist_MainLoop( playlist_t *p_playlist )
{
    playlist_item_t *p_item = NULL;
354
    bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
Laurent Aimar's avatar
Laurent Aimar committed
355 356

    PL_ASSERT_LOCKED;
357

358
    if( pl_priv(p_playlist)->b_reset_currently_playing &&
359
        mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
360
    {
ivoire's avatar
ivoire committed
361
        ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
362
                               get_current_status_item( p_playlist ) );
363
        pl_priv(p_playlist)->last_rebuild_date = mdate();
364
    }
365

366
check_input:
367
    /* If there is an input, check that it doesn't need to die. */
368
    if( pl_priv(p_playlist)->p_input )
369
    {
370
        if( pl_priv(p_playlist)->request.b_request && !pl_priv(p_playlist)->p_input->b_die )
371 372
        {
            PL_DEBUG( "incoming request - stopping current input" );
373
            input_StopThread( pl_priv(p_playlist)->p_input );
374 375
        }

376
        /* This input is dead. Remove it ! */
377
        if( pl_priv(p_playlist)->p_input->b_dead )
378 379 380
        {
            int i_activity;
            input_thread_t *p_input;
381
            sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
382

zorglub's avatar
zorglub committed
383
            PL_DEBUG( "dead input" );
384

385
            p_input = pl_priv(p_playlist)->p_input;
386

387 388 389
            assert( *pp_sout == NULL );
            if( var_CreateGetBool( p_input, "sout-keep" ) )
                *pp_sout = input_DetachSout( p_input );
390 391

            /* Destroy input */
392
            playlist_release_current_input( p_playlist );
393

394
            pl_priv(p_playlist)->gc_date = mdate();
395
            pl_priv(p_playlist)->b_cant_sleep = true;
396

ivoire's avatar
ivoire committed
397
            i_activity= var_GetInteger( p_playlist, "activity" );
398 399
            var_SetInteger( p_playlist, "activity", i_activity -
                            DEFAULT_INPUT_ACTIVITY );
400

401
            goto check_input;
402 403
        }
        /* This input is dying, let it do */
404
        else if( pl_priv(p_playlist)->p_input->b_die )
405
        {
zorglub's avatar
zorglub committed
406
            PL_DEBUG( "dying input" );
407
            PL_UNLOCK;
408
            msleep( INTF_IDLE_SLEEP );
409
            PL_LOCK;
410
            goto check_input;
411 412
        }
        /* This input has finished, ask it to die ! */
413 414
        else if( pl_priv(p_playlist)->p_input->b_error
                  || pl_priv(p_playlist)->p_input->b_eof )
415
        {
zorglub's avatar
zorglub committed
416
            PL_DEBUG( "finished input" );
417
            input_StopThread( pl_priv(p_playlist)->p_input );
418 419
            /* No need to wait here, we'll wait in the p_input->b_die case */
            goto check_input;
420
        }
421
        else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
422
        {
423
            ObjectGarbageCollector( p_playlist, false );
424 425 426 427 428 429 430 431 432
        }
    }
    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
433
        */
434 435
        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
436
        if( i_status != PLAYLIST_STOPPED )
ivoire's avatar
ivoire committed
437 438 439
        {
            msg_Dbg( p_playlist, "starting new item" );
            p_item = playlist_NextItem( p_playlist );
440

ivoire's avatar
ivoire committed
441 442
            if( p_item == NULL )
            {
443
                msg_Dbg( p_playlist, "nothing to play" );
444
                pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
445

446
                if( b_playexit == true )
447 448
                {
                    msg_Info( p_playlist, "end of playlist, exiting" );
449
                    vlc_object_kill( p_playlist->p_libvlc );
450
                }
451
                ObjectGarbageCollector( p_playlist, true );
452
                return;
ivoire's avatar
ivoire committed
453 454
            }
            playlist_PlayItem( p_playlist, p_item );
Laurent Aimar's avatar
Laurent Aimar committed
455
            /* playlist_PlayItem loose input event, we need to recheck */
Laurent Aimar's avatar
Laurent Aimar committed
456
            goto check_input;
ivoire's avatar
ivoire committed
457 458 459
        }
        else
        {
460
            const bool b_gc_forced = pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED;
461

462
            pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
463

464
            /* Collect garbage */
465
            ObjectGarbageCollector( p_playlist, b_gc_forced );
466
        }
467 468 469
    }
}

ivoire's avatar
ivoire committed
470 471 472 473 474 475 476
/**
 * Last loop
 *
 * The playlist is dying so do the last loop
 * \param p_playlist the playlist object
 * \return nothing
*/
477 478 479 480 481
void playlist_LastLoop( playlist_t *p_playlist )
{
    /* If there is an input, kill it */
    while( 1 )
    {
482
        PL_LOCK;
483
        if( pl_priv(p_playlist)->p_input == NULL )
484
        {
485
            PL_UNLOCK;
486 487 488
            break;
        }

489
        if( pl_priv(p_playlist)->p_input->b_dead )
490
        {
491 492
            /* remove input */
            playlist_release_current_input( p_playlist );
493

494 495 496
            /* sout-keep: no need to anything here.
             * The last input will destroy its sout, if any, by itself */

497
            PL_UNLOCK;
498 499
            continue;
        }
500
        else if( pl_priv(p_playlist)->p_input->b_die )
501 502 503 504
        {
            /* This input is dying, leave it alone */
            ;
        }
505
        else if( pl_priv(p_playlist)->p_input->b_error || pl_priv(p_playlist)->p_input->b_eof )
506
        {
507
            input_StopThread( pl_priv(p_playlist)->p_input );
508
            PL_UNLOCK;
509 510 511 512
            continue;
        }
        else
        {
513
            pl_priv(p_playlist)->p_input->b_eof = 1;
514
        }
515
        PL_UNLOCK;
516 517 518 519

        msleep( INTF_IDLE_SLEEP );
    }

520
#ifdef ENABLE_SOUT
521
    /* close the remaining sout-keep (if there was no input atm) */
522
    sout_instance_t *p_sout = pl_priv(p_playlist)->p_sout;
523 524
    if (p_sout)
        sout_DeleteInstance( p_sout );
525
#endif
526

527 528 529 530 531 532 533
    /* 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;

534 535 536 537 538
    /* Release the current node */
    set_current_status_node( p_playlist, NULL );

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

540 541
    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
542
        vlc_gc_decref( p_del->p_input );
543 544 545
        free( p_del );
    FOREACH_END();
    ARRAY_RESET( p_playlist->all_items );
546
    FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
547 548 549 550
        free( p_del->pp_children );
        vlc_gc_decref( p_del->p_input );
        free( p_del );
    FOREACH_END();
551
    ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
552 553 554 555 556

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

    PL_UNLOCK;
557 558
}

ivoire's avatar
ivoire committed
559
/**
560
 * Preparse queue loop
ivoire's avatar
ivoire committed
561
 *
562 563
 * @param p_obj preparse structure
 * @return never
ivoire's avatar
ivoire committed
564
 */
565
void *playlist_PreparseLoop( void *data )
566
{
567
    playlist_preparse_t *p_preparse = data;
568 569
    playlist_t *p_playlist = &((playlist_private_t *)(((char *)p_preparse)
             - offsetof(playlist_private_t, preparse)))->public_data;
570

571
    for( ;; )
572
    {
573
        input_item_t *p_current;
574

575 576 577 578 579 580 581 582 583
        vlc_mutex_lock( &p_preparse->lock );
        mutex_cleanup_push( &p_preparse->lock );

        while( p_preparse->i_waiting == 0 )
            vlc_cond_wait( &p_preparse->wait, &p_preparse->lock );

        p_current = p_preparse->pp_waiting[0];
        REMOVE_ELEM( p_preparse->pp_waiting, p_preparse->i_waiting, 0 );
        vlc_cleanup_run( );
584

585 586
        if( p_current )
        {
587 588
            int canc = vlc_savecancel ();
            PL_LOCK;
589
            if( p_current->i_type == ITEM_TYPE_FILE )
590 591 592
            {
                stats_TimerStart( p_playlist, "Preparse run",
                                  STATS_TIMER_PREPARSE );
593
                /* Do not preparse if it is already done (like by playing it) */
594
                if( !input_item_IsPreparsed( p_current ) )
595 596 597 598 599
                {
                    PL_UNLOCK;
                    input_Preparse( p_playlist, p_current );
                    PL_LOCK;
                }
600
                stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
601
                PL_UNLOCK;
602
                input_item_SetPreparsed( p_current, true );
zorglub's avatar
zorglub committed
603
                var_SetInteger( p_playlist, "item-change", p_current->i_id );
604
                PL_LOCK;
605
            }
zorglub's avatar
zorglub committed
606
            /* If we haven't retrieved enough meta, add to secondary queue
607 608 609
             * which will run the "meta fetchers".
             * This only checks for meta, not for art
             * \todo don't do this for things we won't get meta for, like vids
610
             */
Rafaël Carré's avatar
Rafaël Carré committed
611 612
            char *psz_arturl = input_item_GetArtURL( p_current );
            char *psz_name = input_item_GetName( p_current );
613
            playlist_fetcher_t *p_fetcher = &pl_priv(p_playlist)->fetcher;
614 615
            if( p_fetcher->i_art_policy == ALBUM_ART_ALL &&
                ( !psz_arturl || strncmp( psz_arturl, "file://", 7 ) ) )
616
            {
Rafaël Carré's avatar
Rafaël Carré committed
617
                PL_DEBUG("meta ok for %s, need to fetch art", psz_name );
618 619 620 621 622
                vlc_mutex_lock( &p_fetcher->lock );
                INSERT_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting,
                             p_fetcher->i_waiting, p_current);
                vlc_cond_signal( &p_fetcher->wait );
                vlc_mutex_unlock( &p_fetcher->lock );
zorglub's avatar
zorglub committed
623
            }
624
            else
625 626
            {
                PL_DEBUG( "no fetch required for %s (art currently %s)",
Rafaël Carré's avatar
Rafaël Carré committed
627
                          psz_name, psz_arturl );
zorglub's avatar
zorglub committed
628
                vlc_gc_decref( p_current );
629
            }
Rafaël Carré's avatar
Rafaël Carré committed
630 631
            free( psz_name );
            free( psz_arturl );
632 633
	    PL_UNLOCK;
            vlc_restorecancel( canc );
634
        }
635

636
        int i_activity = var_GetInteger( p_playlist, "activity" );
637
        if( i_activity < 0 ) i_activity = 0;
638
        /* Sleep at least 1ms */
639
        msleep( (i_activity+1) * 1000 );
640 641
    }

642 643
    assert( 0 );
    return NULL;
644 645
}

ivoire's avatar
ivoire committed
646 647 648
/**
 * Fetcher loop
 *
649
 * \return never
ivoire's avatar
ivoire committed
650
 */
651
void *playlist_FetcherLoop( void *data )
652
{
653 654 655
    playlist_fetcher_t *p_fetcher = data;
    playlist_t *p_playlist = &((playlist_private_t *)(((char *)p_fetcher)
             - offsetof(playlist_private_t, fetcher)))->public_data;
656

657
    for( ;; )
658
    {
659 660 661 662 663 664 665
        input_item_t *p_item;

        vlc_mutex_lock( &p_fetcher->lock );
        mutex_cleanup_push( &p_fetcher->lock );

        while( p_fetcher->i_waiting == 0 )
            vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
666

667 668 669 670 671
        p_item = p_fetcher->pp_waiting[0];
        REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
        vlc_cleanup_run( );

        int canc = vlc_savecancel();
dionoea's avatar
dionoea committed
672
        if( p_item )
673
        {
Rafaël Carré's avatar
Rafaël Carré committed
674 675
            int i_ret;

676 677
            /* Check if it is not yet preparsed and if so wait for it
             * (at most 0.5s)
Rafaël Carré's avatar
Rafaël Carré committed
678
             * (This can happen if we fetch art on play)
679 680
             * FIXME this doesn't work if we need to fetch meta before art...
             */
Rafaël Carré's avatar
Rafaël Carré committed
681
            for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
zorglub's avatar
zorglub committed
682
            {
Rafaël Carré's avatar
Rafaël Carré committed
683 684
                bool b_break;
                PL_LOCK;
685 686
                b_break = ( !pl_priv(p_playlist)->p_input || input_GetItem(pl_priv(p_playlist)->p_input) != p_item  ||
                            pl_priv(p_playlist)->p_input->b_die || pl_priv(p_playlist)->p_input->b_eof || pl_priv(p_playlist)->p_input->b_error );
Rafaël Carré's avatar
Rafaël Carré committed
687 688 689 690 691
                PL_UNLOCK;
                if( b_break )
                    break;
                msleep( 50000 );
            }
692

Rafaël Carré's avatar
Rafaël Carré committed
693 694 695 696 697 698 699 700 701 702
            i_ret = input_ArtFind( p_playlist, p_item );
            if( i_ret == 1 )
            {
                PL_DEBUG( "downloading art for %s", p_item->psz_name );
                if( input_DownloadAndCacheArt( p_playlist, p_item ) )
                    input_item_SetArtNotFound( p_item, true );
                else {
                    input_item_SetArtFetched( p_item, true );
                    var_SetInteger( p_playlist, "item-change",
                                    p_item->i_id );
703
                }
Rafaël Carré's avatar
Rafaël Carré committed
704 705 706 707 708 709
            }
            else if( i_ret == 0 ) /* Was in cache */
            {
                PL_DEBUG( "found art for %s in cache", p_item->psz_name );
                input_item_SetArtFetched( p_item, true );
                var_SetInteger( p_playlist, "item-change", p_item->i_id );
zorglub's avatar
zorglub committed
710 711
            }
            else
dionoea's avatar
dionoea committed
712
            {
Rafaël Carré's avatar
Rafaël Carré committed
713 714 715 716
                PL_DEBUG( "art not found for %s", p_item->psz_name );
                input_item_SetArtNotFound( p_item, true );
            }
            vlc_gc_decref( p_item );
717
        }
718 719 720
        vlc_restorecancel( canc );

        int i_activity = var_GetInteger( p_playlist, "activity" );
721 722 723
        if( i_activity < 0 ) i_activity = 0;
        /* Sleep at least 1ms */
        msleep( (i_activity+1) * 1000 );
724 725
    }

726 727
    assert( 0 );
    return NULL;
728 729
}

730 731 732 733 734
static void VariablesInit( playlist_t *p_playlist )
{
    vlc_value_t val;
    /* These variables control updates */
    var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
735
    val.b_bool = true;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    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 */
756 757 758 759 760
    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
761 762

    var_AddCallback( p_playlist, "random", RandomCallback, NULL );
763
}
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

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