engine.c 15.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * engine.c : Run the playlist and handle its control
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5 6 7 8
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser 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.
22
 *****************************************************************************/
23

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

28
#include <stddef.h>
29
#include <assert.h>
30

31
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
32 33 34
#include <vlc_sout.h>
#include <vlc_playlist.h>
#include <vlc_interface.h>
35
#include <vlc_http.h>
36
#include "playlist_internal.h"
37
#include "input/resource.h"
38 39 40 41 42 43

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void VariablesInit( playlist_t *p_playlist );

Clément Stenac's avatar
Clément Stenac committed
44 45 46
static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
                           vlc_value_t oldval, vlc_value_t newval, void *a )
{
47
    (void)psz_cmd; (void)oldval; (void)newval; (void)a;
48
    playlist_t *p_playlist = (playlist_t*)p_this;
49
    bool random = newval.b_bool;
50

51 52
    PL_LOCK;

53 54 55 56 57 58 59 60 61 62 63 64 65 66
    if( !random ) {
        pl_priv(p_playlist)->b_reset_currently_playing = true;
        vlc_cond_signal( &pl_priv(p_playlist)->signal );
    } else {
        /* Shuffle and sync the playlist on activation of random mode.
         * This preserves the current playing item, so that the user
         * can return to it if needed. (See #4472)
         */
        playlist_private_t *p_sys = pl_priv(p_playlist);
        playlist_item_t *p_new = p_sys->status.p_item;
        ResetCurrentlyPlaying( p_playlist, NULL );
        if( p_new )
            ResyncCurrentIndex( p_playlist, p_new );
    }
67 68

    PL_UNLOCK;
Clément Stenac's avatar
Clément Stenac committed
69 70 71
    return VLC_SUCCESS;
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
 * When there are one or more pending corks, playback should be paused.
 * This is used for audio policy.
 * \warning Always add and remove a cork with var_IncInteger() and var_DecInteger().
 * var_Get() and var_Set() are prone to race conditions.
 */
static int CorksCallback( vlc_object_t *obj, char const *var,
                          vlc_value_t old, vlc_value_t cur, void *dummy )
{
    playlist_t *pl = (playlist_t *)obj;

    msg_Dbg( obj, "corks count: %"PRId64" -> %"PRId64, old.i_int, cur.i_int );
    if( !old.i_int == !cur.i_int )
        return VLC_SUCCESS; /* nothing to do */

87 88 89
    if( !var_InheritBool( obj, "playlist-cork" ) )
        return VLC_SUCCESS;

90 91
    if( cur.i_int )
    {
92 93
        msg_Dbg( obj, "corked" );
        playlist_Pause( pl );
94 95
    }
    else
96
    {
97
        msg_Dbg( obj, "uncorked" );
98 99
        playlist_Resume( pl );
    }
100 101 102 103 104

    (void) var; (void) dummy;
    return VLC_SUCCESS;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p )
{
    (void)psz_cmd; (void)oldval;(void)p;
    playlist_t *p_playlist = (playlist_t*)p_this;

    PL_LOCK;

    if( pl_priv(p_playlist)->p_input == NULL )
    {
        PL_UNLOCK;
        return VLC_SUCCESS;
    }

    var_SetFloat( pl_priv( p_playlist )->p_input, "rate", newval.f_float );
    PL_UNLOCK;
    return VLC_SUCCESS;
}

124
static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
125 126
                               vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
127
    playlist_t *p_playlist = (playlist_t *)obj;
128 129 130 131 132 133 134
    VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);

    static const float pf_rate[] = {
        1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
        1.0/1,
        3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
    };
135 136 137 138
    const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);

    float f_rate;
    struct input_thread_t *input;
139 140

    PL_LOCK;
141 142
    input = pl_priv( p_playlist )->p_input;
    f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
143 144
    PL_UNLOCK;

145
    if( !strcmp( psz_cmd, "rate-faster" ) )
146
    {
147
        /* compensate for input rounding errors */
148
        float r = f_rate * 1.1f;
149 150 151 152 153 154
        for( size_t i = 0; i < i_rate_count; i++ )
            if( r < pf_rate[i] )
            {
                f_rate = pf_rate[i];
                break;
            }
155
    }
156
    else
157
    {
158
        /* compensate for input rounding errors */
159
        float r = f_rate * .9f;
160 161 162 163 164 165
        for( size_t i = 1; i < i_rate_count; i++ )
            if( r <= pf_rate[i] )
            {
                f_rate = pf_rate[i - 1];
                break;
            }
166
    }
167 168

    var_SetFloat( p_playlist, "rate", f_rate );
169 170 171
    return VLC_SUCCESS;
}

172 173 174 175
static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,
                                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    playlist_t *p_playlist = (playlist_t*)p_this;
176
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
177 178 179 180 181 182

    PL_LOCK;

    /* Force the input to restart the video ES to force a vout recreation */
    input_thread_t *p_input = pl_priv( p_playlist )->p_input;
    if( p_input )
183 184
    {
        const double f_position = var_GetFloat( p_input, "position" );
185
        input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
186 187
        var_SetFloat( p_input, "position", f_position );
    }
188 189 190 191

    PL_UNLOCK;
    return VLC_SUCCESS;
}
192

193 194 195 196 197 198 199
/**
 * 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
 */
200
playlist_t *playlist_Create( vlc_object_t *p_parent )
201 202
{
    playlist_t *p_playlist;
203
    playlist_private_t *p;
204 205

    /* Allocate structure */
206
    p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" );
207
    if( !p )
208
        return NULL;
209

210 211
    assert( offsetof( playlist_private_t, public_data ) == 0 );
    p_playlist = &p->public_data;
212
    TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
213

214
    VariablesInit( p_playlist );
215
    vlc_mutex_init( &p->lock );
216
    vlc_cond_init( &p->signal );
217
    p->killed = false;
218 219

    /* Initialise data structures */
220
    pl_priv(p_playlist)->i_last_playlist_id = 0;
221
    pl_priv(p_playlist)->p_input = NULL;
222

Clément Stenac's avatar
Clément Stenac committed
223 224
    ARRAY_INIT( p_playlist->items );
    ARRAY_INIT( p_playlist->all_items );
225
    ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
Clément Stenac's avatar
Clément Stenac committed
226
    ARRAY_INIT( p_playlist->current );
227

Clément Stenac's avatar
Clément Stenac committed
228
    p_playlist->i_current_index = 0;
229
    pl_priv(p_playlist)->b_reset_currently_playing = true;
Clément Stenac's avatar
Clément Stenac committed
230

231
    pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
232
    pl_priv(p_playlist)->b_preparse = var_InheritBool( p_parent, "auto-preparse" );
233

234 235
    /* Create the root, playing items and meida library nodes */
    playlist_item_t *root, *playing, *ml;
236

237
    PL_LOCK;
238 239 240
    root = playlist_NodeCreate( p_playlist, NULL, NULL,
                                PLAYLIST_END, 0, NULL );
    playing = playlist_NodeCreate( p_playlist, _( "Playlist" ), root,
241
                                   PLAYLIST_END, PLAYLIST_RO_FLAG | PLAYLIST_NO_INHERIT_FLAG, NULL );
242 243
    if( var_InheritBool( p_parent, "media-library") )
        ml = playlist_NodeCreate( p_playlist, _( "Media Library" ), root,
244
                                  PLAYLIST_END, PLAYLIST_RO_FLAG | PLAYLIST_NO_INHERIT_FLAG, NULL );
245 246
    else
        ml = NULL;
247
    PL_UNLOCK;
248

249
    if( unlikely(root == NULL || playing == NULL) )
250
        abort();
251

252 253 254
    p_playlist->p_root = root;
    p_playlist->p_playing = playing;
    p_playlist->p_media_library = ml;
255

256
    /* Initial status */
257
    pl_priv(p_playlist)->status.p_item = NULL;
258
    pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
259
    pl_priv(p_playlist)->request.b_request = false;
260

261
    if (ml != NULL)
262
        playlist_MLLoad( p_playlist );
263

264 265 266 267 268
    /* Input resources */
    p->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
    if( unlikely(p->p_input_resource == NULL) )
        abort();

269 270 271 272 273
    /* Audio output (needed for volume and device controls). */
    audio_output_t *aout = input_resource_GetAout( p->p_input_resource );
    if( aout != NULL )
        input_resource_PutAout( p->p_input_resource, aout );

274 275 276 277 278 279 280 281 282
    /* Initialize the shared HTTP cookie jar */
    vlc_value_t cookies;
    cookies.p_address = vlc_http_cookies_new();
    if ( likely(cookies.p_address) )
    {
        var_Create( p_playlist, "http-cookies", VLC_VAR_ADDRESS );
        var_SetChecked( p_playlist, "http-cookies", VLC_VAR_ADDRESS, cookies );
    }

283 284 285
    /* Thread */
    playlist_Activate (p_playlist);

286 287 288 289
    /* Add service discovery modules */
    char *mods = var_InheritString( p_playlist, "services-discovery" );
    if( mods != NULL )
    {
290 291
        char *s = mods, *m;
        while( (m = strsep( &s, " :," )) != NULL )
292 293 294 295
            playlist_ServicesDiscoveryAdd( p_playlist, m );
        free( mods );
    }

296 297 298
    return p_playlist;
}

299 300 301 302 303 304 305
/**
 * Destroy playlist.
 * This is not thread-safe. Any reference to the playlist is assumed gone.
 * (In particular, all interface and services threads must have been joined).
 *
 * \param p_playlist the playlist object
 */
306 307 308 309
void playlist_Destroy( playlist_t *p_playlist )
{
    playlist_private_t *p_sys = pl_priv(p_playlist);

310 311 312
    /* Remove all services discovery */
    playlist_ServicesDiscoveryKillAll( p_playlist );

313
    msg_Dbg( p_playlist, "destroying" );
314

315
    playlist_Deactivate( p_playlist );
316

317 318 319 320 321 322 323 324 325 326 327 328 329
    /* Release input resources */
    assert( p_sys->p_input == NULL );
    input_resource_Release( p_sys->p_input_resource );

    if( p_playlist->p_media_library != NULL )
        playlist_MLDump( p_playlist );

    PL_LOCK;
    /* Release the current node */
    set_current_status_node( p_playlist, NULL );
    /* Release the current item */
    set_current_status_item( p_playlist, NULL );
    PL_UNLOCK;
330

331
    vlc_cond_destroy( &p_sys->signal );
332
    vlc_mutex_destroy( &p_sys->lock );
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

    /* 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 );

351 352 353 354 355 356 357
    vlc_http_cookie_jar_t *cookies = var_GetAddress( p_playlist, "http-cookies" );
    if ( cookies )
    {
        var_Destroy( p_playlist, "http-cookies" );
        vlc_http_cookies_destroy( cookies );
    }

358
    vlc_object_release( p_playlist );
359 360
}

361 362 363 364 365 366
/** Get current playing input.
 */
input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
{
    input_thread_t * p_input;
    PL_LOCK;
367
    p_input = pl_priv(p_playlist)->p_input;
368
    if( p_input ) vlc_object_hold( p_input );
369 370 371 372
    PL_UNLOCK;
    return p_input;
}

373 374 375 376
/**
 * @}
 */

377 378 379 380 381 382
/** Accessor for status item and status nodes.
 */
playlist_item_t * get_current_status_item( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

383
    return pl_priv(p_playlist)->status.p_item;
384 385 386 387 388 389
}

playlist_item_t * get_current_status_node( playlist_t * p_playlist )
{
    PL_ASSERT_LOCKED;

390
    return pl_priv(p_playlist)->status.p_node;
391 392 393 394 395 396 397
}

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

398 399 400
    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 )
401
    {
402
        /* It's unsafe given current design to delete a playlist item :(
403
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
404
    }
405
    pl_priv(p_playlist)->status.p_item = p_item;
406 407 408 409 410 411 412
}

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

413 414 415
    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 )
416
    {
417
        /* It's unsafe given current design to delete a playlist item :(
418
        playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
419
    }
420
    pl_priv(p_playlist)->status.p_node = p_node;
421 422
}

423 424 425 426
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
427
    var_SetBool( p_playlist, "intf-change", true );
428

429
    var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
430
    var_Create( p_playlist, "leaf-to-parent", VLC_VAR_INTEGER );
431

432 433
    var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
    var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
434

435
    var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
436

437
    var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
438 439

    /* Variables to control playback */
440
    var_Create( p_playlist, "playlist-autostart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
441
    var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
442
    var_AddCallback( p_playlist, "random", RandomCallback, NULL );
443 444
    var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
445 446
    var_Create( p_playlist, "corks", VLC_VAR_INTEGER );
    var_AddCallback( p_playlist, "corks", CorksCallback, NULL );
Clément Stenac's avatar
Clément Stenac committed
447

448 449
    var_Create( p_playlist, "rate", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
    var_AddCallback( p_playlist, "rate", RateCallback, NULL );
450
    var_Create( p_playlist, "rate-slower", VLC_VAR_VOID );
451
    var_AddCallback( p_playlist, "rate-slower", RateOffsetCallback, NULL );
452
    var_Create( p_playlist, "rate-faster", VLC_VAR_VOID );
453 454
    var_AddCallback( p_playlist, "rate-faster", RateOffsetCallback, NULL );

455 456
    var_Create( p_playlist, "video-splitter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_AddCallback( p_playlist, "video-splitter", VideoSplitterCallback, NULL );
457

458 459
    /* sout variables */
    var_Create( p_playlist, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
460
    var_Create( p_playlist, "demux-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
461

462
    /* */
463
    var_Create( p_playlist, "metadata-network-access", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
464 465 466

    /* Variables to preserve video output parameters */
    var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
467
    var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
468
    var_Create( p_playlist, "video-wallpaper", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
469

470
    /* Audio output parameters */
471
    var_Create( p_playlist, "audio-device", VLC_VAR_STRING );
472
    var_Create( p_playlist, "mute", VLC_VAR_BOOL );
473 474
    var_Create( p_playlist, "volume", VLC_VAR_FLOAT );
    var_SetFloat( p_playlist, "volume", -1.f );
475
}
476 477 478

playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
{
479 480
    PL_ASSERT_LOCKED;

481 482 483 484 485
    return pl_priv(p_playlist)->status.p_item;
}

int playlist_Status( playlist_t * p_playlist )
{
486 487
    input_thread_t *p_input = pl_priv(p_playlist)->p_input;

488 489
    PL_ASSERT_LOCKED;

490 491 492 493 494
    if( p_input == NULL )
        return PLAYLIST_STOPPED;
    if( var_GetInteger( p_input, "state" ) == PAUSE_S )
        return PLAYLIST_PAUSED;
    return PLAYLIST_RUNNING;
495
}
496