item.c 29 KB
Newer Older
1
/*****************************************************************************
2
 * item.c : Playlist item creation/deletion/add/removal functions
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2007 VLC authors and VideoLAN
Clément Stenac's avatar
Clément Stenac committed
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Clément Stenac <zorglub@videolan.org>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * 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
13 14 15 16
 * (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
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23
 *****************************************************************************/
24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
29
#include <assert.h>
30
#include <vlc_playlist.h>
31
#include <vlc_rand.h>
32
#include "playlist_internal.h"
33

34 35 36
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_mode, int i_pos );
static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
37
                           playlist_item_t * );
38
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
39

40 41 42 43
static int RecursiveAddIntoParent (
                playlist_t *p_playlist, playlist_item_t *p_parent,
                input_item_node_t *p_node, int i_pos, bool b_flat,
                playlist_item_t **pp_first_leaf );
44 45 46
static int RecursiveInsertCopy (
                playlist_t *p_playlist, playlist_item_t *p_item,
                playlist_item_t *p_parent, int i_pos, bool b_flat );
47

48
/*****************************************************************************
49
 * An input item has gained subitems (Event Callback)
50 51
 *****************************************************************************/

52 53 54 55 56
static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
                                          void * user_data )
{
    input_item_t *p_input = p_event->p_obj;
    playlist_t *p_playlist = (( playlist_item_t* ) user_data)->p_playlist;
57
    playlist_private_t *p_sys = pl_priv( p_playlist );
58
    input_item_node_t *p_new_root = p_event->u.input_item_subitem_tree_added.p_root;
59

60 61
    PL_LOCK;

62 63 64 65
    playlist_item_t *p_item =
        playlist_ItemGetByInput( p_playlist, p_input );

    assert( p_item != NULL );
66

67
    bool b_current = get_current_status_item( p_playlist ) == p_item;
68
    bool b_autostart = var_GetBool( p_playlist, "playlist-autostart" );
69
    bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
70 71
    bool b_flat = false;

72
    p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
Pierre d'Herbemont's avatar
(oops).  
Pierre d'Herbemont committed
73

74 75 76 77 78 79
    /* We will have to flatten the tree out if we are in "the playlist" node and
    the user setting demands flat playlist */

    if( !pl_priv(p_playlist)->b_tree ) {
        playlist_item_t *p_up = p_item;
        while( p_up->p_parent )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
80
        {
81 82 83 84 85 86
            if( p_up->p_parent == p_playlist->p_playing )
            {
                b_flat = true;
                break;
            }
            p_up = p_up->p_parent;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
87
        }
88
    }
89

90 91 92 93 94
    int pos = 0;

    /* If we have to flatten out, then take the item's position in the parent as
    insertion point and delete the item */

95 96
    bool b_redirect_request = false;

97
    if( b_flat )
98
    {
99 100 101 102 103
        playlist_item_t *p_parent = p_item->p_parent;
        assert( p_parent != NULL );

        int i;
        for( i = 0; i < p_parent->i_children; i++ )
104
        {
105 106 107 108 109
            if( p_parent->pp_children[i] == p_item )
            {
                pos = i;
                break;
            }
110
        }
111
        assert( i < p_parent->i_children );
Jakob Leben's avatar
Jakob Leben committed
112

113
        playlist_NodeDelete( p_playlist, p_item, false );
Jakob Leben's avatar
Jakob Leben committed
114

115 116 117 118 119 120 121 122 123 124 125
        /* If there is a pending request referring to the item we just deleted
         * it needs to be updated so that we do not try to play an entity that
         * is no longer part of the playlist. */

        if( p_sys->request.b_request &&
            ( p_sys->request.p_item == p_item ||
              p_sys->request.p_node == p_item ) )
        {
            b_redirect_request = true;
        }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        p_item = p_parent;
    }
    else
    {
        pos = p_item->i_children >= 0 ? p_item->i_children : 0;
    }

    /* At this point:
    "p_item" is the node where sub-items should be inserted,
    "pos" is the insertion position in that node */

    int last_pos = playlist_InsertInputItemTree( p_playlist,
                                                 p_item,
                                                 p_new_root,
                                                 pos,
                                                 b_flat );
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if( b_redirect_request )
    {
        /* a redirect of the pending request is required, as such we update the
         * request to refer to the item that would have been the next in line
         * (if any). */

        assert( b_flat );

        playlist_item_t* p_redirect = NULL;

        if( p_item->i_children > pos )
            p_redirect = p_item->pp_children[pos];

        p_sys->request.p_item = p_redirect;
        p_sys->request.p_node = NULL;
    }
158

159
    if( !b_flat ) var_SetInteger( p_playlist, "leaf-to-parent", p_item->i_id );
160

161
    //control playback only if it was the current playing item that got subitems
162
    if( b_current )
163
    {
164
        if( last_pos == pos || ( b_stop && !b_flat ) || !b_autostart )
165
        {
166 167
            /* We stop, either because no sub-item was actually created, or some
            flags/settings want us to do so at this point */
168 169 170 171 172 173
            PL_UNLOCK;
            playlist_Stop( p_playlist );
            return;
        }
        else
        {
174 175 176 177 178
            /* Continue to play, either random or the first new item */
            playlist_item_t *p_play_item;

            if( var_GetBool( p_playlist, "random" ) )
            {
179
                p_play_item = NULL;
180 181 182
            }
            else
            {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                /* Don't Play a directory if it can loop into a parent */
                if( p_new_root->b_can_loop )
                {
                    /* Play the first regular file */
                    for( ; pos < last_pos; pos++ )
                    {
                        if( p_item->pp_children[pos]->p_input->i_type != ITEM_TYPE_DIRECTORY )
                            break;
                    }
                    if( last_pos == pos )
                    {
                        PL_UNLOCK;
                        playlist_Stop( p_playlist );
                        return;
                    }
                }
199 200 201 202 203 204 205 206 207 208
                p_play_item = p_item->pp_children[pos];
                /* NOTE: this is a work around the general bug:
                if node-to-be-played contains sub-nodes, then second instead
                of first leaf starts playing (only in case the leafs have just
                been instered and playlist has not yet been rebuilt.)
                */
                while( p_play_item->i_children > 0 )
                    p_play_item = p_play_item->pp_children[0];
            }

209
            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
210 211 212
                                  pl_Locked,
                                  get_current_status_node( p_playlist ),
                                  p_play_item );
213
        }
214
    }
215

216
    PL_UNLOCK;
217
}
218
/*****************************************************************************
219
 * An input item's meta or duration has changed (Event Callback)
220
 *****************************************************************************/
221 222
static void input_item_changed( const vlc_event_t * p_event,
                                void * user_data )
223
{
224 225
    playlist_item_t *p_item = user_data;
    VLC_UNUSED( p_event );
226
    var_SetAddress( p_item->p_playlist, "item-change", p_item->p_input );
227 228
}

229 230 231
/*****************************************************************************
 * Listen to vlc_InputItemAddSubItem event
 *****************************************************************************/
232
static void install_input_item_observer( playlist_item_t * p_item )
233
{
234
    vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
235 236
    vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
                      input_item_add_subitem_tree, p_item );
237 238 239 240
    vlc_event_attach( p_em, vlc_InputItemDurationChanged,
                      input_item_changed, p_item );
    vlc_event_attach( p_em, vlc_InputItemMetaChanged,
                      input_item_changed, p_item );
241 242 243 244
    vlc_event_attach( p_em, vlc_InputItemNameChanged,
                      input_item_changed, p_item );
    vlc_event_attach( p_em, vlc_InputItemInfoChanged,
                      input_item_changed, p_item );
245 246
    vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
                      input_item_changed, p_item );
247 248
}

249
static void uninstall_input_item_observer( playlist_item_t * p_item )
250
{
251
    vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
252 253
    vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
                      input_item_add_subitem_tree, p_item );
254 255 256 257
    vlc_event_detach( p_em, vlc_InputItemMetaChanged,
                      input_item_changed, p_item );
    vlc_event_detach( p_em, vlc_InputItemDurationChanged,
                      input_item_changed, p_item );
258
    vlc_event_detach( p_em, vlc_InputItemNameChanged,
259
                      input_item_changed, p_item );
260
    vlc_event_detach( p_em, vlc_InputItemInfoChanged,
261
                      input_item_changed, p_item );
262 263
    vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
                      input_item_changed, p_item );
264 265
}

266 267 268
/*****************************************************************************
 * Playlist item creation
 *****************************************************************************/
269
playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
270
                                              input_item_t *p_input )
271
{
272 273 274
    playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
    if( !p_item )
        return NULL;
275

276 277
    assert( p_input );

278 279
    p_item->p_input = p_input;
    vlc_gc_incref( p_item->p_input );
280

281
    p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
282

283
    p_item->p_parent = NULL;
284 285
    p_item->i_children = -1;
    p_item->pp_children = NULL;
286
    p_item->i_nb_played = 0;
287
    p_item->i_flags = 0;
288
    p_item->p_playlist = p_playlist;
289

290
    install_input_item_observer( p_item );
291

292 293 294
    return p_item;
}

295 296 297 298
/***************************************************************************
 * Playlist item destruction
 ***************************************************************************/

299
/**
300
 * Release an item
301 302 303
 *
 * \param p_item item to delete
*/
304
void playlist_ItemRelease( playlist_item_t *p_item )
305
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
306 307
    /* For the assert */
    playlist_t *p_playlist = p_item->p_playlist;
308 309
    PL_ASSERT_LOCKED;

310
    uninstall_input_item_observer( p_item );
311 312 313
    free( p_item->pp_children );
    vlc_gc_decref( p_item->p_input );
    free( p_item );
314 315
}

316 317 318 319 320 321 322
/**
 * Clear the playlist
 *
 * \param p_playlist playlist object
 * \param b_locked TRUE if the playlist is locked
 * \return nothing
 */
323
void playlist_Clear( playlist_t * p_playlist, bool b_locked )
324
{
325 326
    playlist_item_t *p_root = p_playlist->p_playing;

327
    PL_LOCK_IF( !b_locked );
328 329 330 331

    for( int i = p_root->i_children - 1; i >= 0 ;i-- )
        playlist_NodeDelete( p_playlist, p_root->pp_children[i], false );

332
    PL_UNLOCK_IF( !b_locked );
333
}
334

335 336 337
/***************************************************************************
 * Playlist item addition
 ***************************************************************************/
338 339 340 341
/**
 * Playlist add
 *
 * Add an item to the playlist or the media library
342 343 344 345 346 347
 * \param p_playlist the playlist to add into
 * \param psz_uri the mrl to add to the playlist
 * \param psz_name a text giving a name or description of this item
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
348
 *        regardless of its size
349
 * \param b_playlist TRUE for playlist, FALSE for media library
350
 * \return VLC_SUCCESS or a VLC error code
351
 */
352 353
int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
                  const char *psz_name, int i_mode, int i_pos,
354
                  bool b_playlist )
355
{
356
    return playlist_AddExt( p_playlist, psz_uri, psz_name,
357
                            i_mode, i_pos, -1, 0, NULL, 0, b_playlist );
358
}
359

360
/**
361
 * Add a MRL into the playlist or the media library, duration and options given
362 363 364 365 366 367 368
 *
 * \param p_playlist the playlist to add into
 * \param psz_uri the mrl to add to the playlist
 * \param psz_name a text giving a name or description of this item
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
369
 *        regardless of its size
370 371
 * \param i_duration length of the item in milliseconds.
 * \param i_options the number of options
372 373
 * \param ppsz_options an array of options
 * \param i_option_flags options flags
374
 * \param b_playlist TRUE for playlist, FALSE for media library
375
 * \return VLC_SUCCESS or a VLC error code
376
*/
377 378
int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
                     const char *psz_name, int i_mode, int i_pos,
379
                     mtime_t i_duration,
380 381
                     int i_options, const char *const *ppsz_options,
                     unsigned i_option_flags,
382
                     bool b_playlist )
383
{
384
    int i_ret;
385
    input_item_t *p_input;
386

387 388
    p_input = input_item_NewExt( psz_uri, psz_name, i_duration,
                                 ITEM_TYPE_UNKNOWN, ITEM_NET_UNKNOWN );
389 390
    if( p_input == NULL )
        return VLC_ENOMEM;
391
    input_item_AddOptions( p_input, i_options, ppsz_options, i_option_flags );
392
    i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos,
393
                               b_playlist );
394
    vlc_gc_decref( p_input );
395
    return i_ret;
396
}
397

398 399 400 401 402 403 404 405 406 407
/**
 * Add an input item to the playlist node
 *
 * \param p_playlist the playlist to add into
 * \param p_input the input item to add
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of its size
 * \param b_playlist TRUE for playlist, FALSE for media library
408
 * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
409
*/
410
int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
411
                       int i_mode, int i_pos, bool b_playlist )
412
{
413
    playlist_item_t *item;
414

415
    PL_LOCK;
416 417
    item = b_playlist ? p_playlist->p_playing
                      : p_playlist->p_media_library;
418

419
    item = playlist_NodeAddInput( p_playlist, p_input, item, i_mode, i_pos );
420
    PL_UNLOCK;
421
    return (item != NULL) ? VLC_SUCCESS : VLC_ENOMEM;
422
}
Gildas Bazin's avatar
 
Gildas Bazin committed
423

424 425 426 427 428 429 430 431 432 433 434 435
/**
 * Add an input item to a given node
 *
 * \param p_playlist the playlist to add into
 * \param p_input the input item to add
 * \param p_parent the parent item to add into
 * \param i_mode the mode used when addin
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of its size
 * \return the new playlist item
 */
436 437 438
playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
                                         input_item_t *p_input,
                                         playlist_item_t *p_parent,
439
                                         int i_mode, int i_pos )
Gildas Bazin's avatar
 
Gildas Bazin committed
440
{
441 442
    PL_ASSERT_LOCKED;

Clément Stenac's avatar
Clément Stenac committed
443 444
    assert( p_input );
    assert( p_parent && p_parent->i_children != -1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
445

446 447 448 449 450 451
    playlist_item_t *p_item = playlist_ItemNewFromInput( p_playlist, p_input );
    if( likely(p_item != NULL) )
    {
        AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
        GoAndPreparse( p_playlist, i_mode, p_item );
    }
452
    return p_item;
Gildas Bazin's avatar
 
Gildas Bazin committed
453
}
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/**
 * Copy an item (and all its children, if any) into another node
 *
 * \param p_playlist the playlist to operate on
 * \param p_item the playlist item to copy
 * \param p_parent the parent item to copy into
 * \param i_pos the position in the parent item for the new copy;
 *              if this is PLAYLIST_END, the copy is appended after all
 *              parent's children
 * \return the position in parent item just behind the last new item inserted
 */
int playlist_NodeAddCopy (
    playlist_t *p_playlist, playlist_item_t *p_item,
    playlist_item_t *p_parent, int i_pos )
{
    PL_ASSERT_LOCKED;
    assert( p_parent != NULL && p_item != NULL );
    assert( p_parent->i_children > -1 );

    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;

    bool b_flat = false;

    playlist_item_t *p_up = p_parent;
    while( p_up )
    {
        if( p_up == p_playlist->p_playing )
            if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
        if( p_up == p_item )
            /* TODO: We don't support copying a node into itself (yet),
            because we insert items as we copy. Instead, we should copy
            all items first and then insert. */
            return i_pos;
        p_up = p_up->p_parent;
    }

    return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
}

494 495 496 497 498 499 500 501 502 503 504 505 506
/**
 * Insert a tree of input items into a given playlist node
 *
 * \param p_playlist the playlist to insert into
 * \param p_parent the receiving playlist node (can be an item)
 * \param p_node the root of input item tree,
          only it's contents will be inserted
 * \param i_pos the position in the playlist where to insert. If this is
 *        PLAYLIST_END the items will be added at the end of the playlist
 *        regardless of its size
 * \param b_flat TRUE if the new tree contents should be flattened into a list
 * \return the first new leaf inserted (in playing order)
 */
507
int playlist_InsertInputItemTree (
508 509 510
    playlist_t *p_playlist, playlist_item_t *p_parent,
    input_item_node_t *p_node, int i_pos, bool b_flat )
{
511 512
    playlist_item_t *p_first_leaf = NULL;
    return RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
513 514
}

515

516 517 518 519
/*****************************************************************************
 * Playlist item misc operations
 *****************************************************************************/

520 521 522 523
/**
 * Find an item within a root, given its input id.
 *
 * \param p_playlist the playlist object
524
 * \param p_item the input item
525
 * \param p_root root playlist item
526 527 528
 * \return the first found item, or NULL if not found
 */
playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
529
                                                    input_item_t *p_item,
530
                                                    playlist_item_t *p_root )
531
{
532
    for( int i = 0 ; i< p_root->i_children ; i++ )
533
    {
534
        if( p_root->pp_children[i]->p_input == p_item )
535 536 537 538 539 540
        {
            return p_root->pp_children[i];
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
            playlist_item_t *p_search =
541
                 playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
542
                                                    p_root->pp_children[i] );
543 544 545 546 547
            if( p_search ) return p_search;
        }
    }
    return NULL;
}
548 549


550
static int ItemIndex ( playlist_item_t *p_item )
Clément Stenac's avatar
Clément Stenac committed
551
{
552 553 554 555 556 557 558 559
    int idx;

    TAB_FIND( p_item->p_parent->i_children,
              p_item->p_parent->pp_children,
              p_item,
              idx );

    return idx;
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
}

/**
 * Moves an item
 *
 * This function must be entered with the playlist lock
 *
 * \param p_playlist the playlist
 * \param p_item the item to move
 * \param p_node the new parent of the item
 * \param i_newpos the new position under this new parent
 * \return VLC_SUCCESS or an error
 */
int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
                       playlist_item_t *p_node, int i_newpos )
{
    PL_ASSERT_LOCKED;
577

Clément Stenac's avatar
Clément Stenac committed
578 579
    if( p_node->i_children == -1 ) return VLC_EGENERIC;

580 581
    playlist_item_t *p_detach = p_item->p_parent;
    int i_index = ItemIndex( p_item );
Clément Stenac's avatar
Clément Stenac committed
582

583 584 585
    REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );

    if( p_detach == p_node && i_index < i_newpos )
Rémi Duraffort's avatar
Rémi Duraffort committed
586 587
        i_newpos--;

Clément Stenac's avatar
Clément Stenac committed
588 589 590
    INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
    p_item->p_parent = p_node;

591 592
    pl_priv( p_playlist )->b_reset_currently_playing = true;
    vlc_cond_signal( &pl_priv( p_playlist )->signal );
Clément Stenac's avatar
Clément Stenac committed
593 594 595
    return VLC_SUCCESS;
}

596
/**
597
 * Moves an array of items
598 599
 *
 * This function must be entered with the playlist lock
600
 *
601
 * \param p_playlist the playlist
602 603 604 605
 * \param i_items the number of indexes to move
 * \param pp_items the array of indexes to move
 * \param p_node the target node
 * \param i_newpos the target position under this node
606
 * \return VLC_SUCCESS or an error
607
 */
608 609 610
int playlist_TreeMoveMany( playlist_t *p_playlist,
                            int i_items, playlist_item_t **pp_items,
                            playlist_item_t *p_node, int i_newpos )
611
{
612 613
    PL_ASSERT_LOCKED;

614 615
    if ( p_node->i_children == -1 ) return VLC_EGENERIC;

616
    for( int i = 0; i < i_items; i++ )
617 618 619 620 621 622 623
    {
        playlist_item_t *p_item = pp_items[i];
        int i_index = ItemIndex( p_item );
        playlist_item_t *p_parent = p_item->p_parent;
        REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
        if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
    }
624
    for( int i = i_items - 1; i >= 0; i-- )
625 626 627 628 629 630 631 632 633
    {
        playlist_item_t *p_item = pp_items[i];
        INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
        p_item->p_parent = p_node;
    }

    pl_priv( p_playlist )->b_reset_currently_playing = true;
    vlc_cond_signal( &pl_priv( p_playlist )->signal );
    return VLC_SUCCESS;
634 635
}

636 637
/**
 * Send a notification that an item has been added to a node
Rémi Duraffort's avatar
Rémi Duraffort committed
638
 *
639 640
 * \param p_playlist the playlist object
 * \param i_item_id id of the item added
641
 * \param i_node_id id of the node in which the item was added
642 643 644
 * \param b_signal TRUE if the function must send a signal
 * \return nothing
 */
645 646
void playlist_SendAddNotify( playlist_t *p_playlist, playlist_item_t *item,
                             bool b_signal )
647
{
648
    playlist_private_t *p_sys = pl_priv(p_playlist);
649 650
    PL_ASSERT_LOCKED;

651
    p_sys->b_reset_currently_playing = true;
652
    if( b_signal )
653 654
        vlc_cond_signal( &p_sys->signal );

655
    var_SetAddress( p_playlist, "playlist-item-append", item );
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
}

/**
 * Get the duration of all items in a node.
 */
mtime_t playlist_GetNodeDuration( playlist_item_t* node )
{
    /* For the assert */
    playlist_t *p_playlist = node->p_playlist;
    PL_ASSERT_LOCKED;

    mtime_t mt_duration = 0;

    if( node->i_children != -1 )
        for( int i = 0; i < node->i_children; i++ )
671 672 673 674 675 676 677
        {
            input_item_t* p_input = node->pp_children[i]->p_input;
            if ( p_input->i_type == ITEM_TYPE_NODE )
                mt_duration += playlist_GetNodeDuration( node->pp_children[i] );
            else
                mt_duration += input_item_GetDuration( p_input );
        }
678 679

    return mt_duration;
680 681 682 683 684 685 686
}

/***************************************************************************
 * The following functions are local
 ***************************************************************************/

/* Enqueue an item for preparsing, and play it, if needed */
687
static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
688
                           playlist_item_t *p_item )
689
{
690 691
    playlist_private_t *sys = pl_priv(p_playlist);

692
    PL_ASSERT_LOCKED;
Clément Stenac's avatar
Clément Stenac committed
693
    if( (i_mode & PLAYLIST_GO ) )
694
    {
695 696 697 698
        sys->request.b_request = true;
        sys->request.i_skip = 0;
        sys->request.p_item = p_item;
        if( sys->p_input != NULL )
699
            input_Stop( sys->p_input );
700
        vlc_cond_signal( &sys->signal );
701
    }
702
    /* Preparse if no artist/album info, and hasn't been preparsed already
703 704
       and if user has some preparsing option (auto-preparse variable)
       enabled*/
705 706
    char *psz_artist = input_item_GetArtist( p_item->p_input );
    char *psz_album = input_item_GetAlbum( p_item->p_input );
707 708

    if( sys->b_preparse && !input_item_IsPreparsed( p_item->p_input )
709
     && (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album)) )
710 711
        libvlc_MetadataRequest( p_playlist->obj.libvlc, p_item->p_input, 0, -1,
                                NULL );
Rafaël Carré's avatar
Rafaël Carré committed
712 713
    free( psz_artist );
    free( psz_album );
714 715 716
}

/* Add the playlist item to the requested node and fire a notification */
717 718
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_mode, int i_pos )
719
{
720
    PL_ASSERT_LOCKED;
Clément Stenac's avatar
Clément Stenac committed
721
    ARRAY_APPEND(p_playlist->items, p_item);
722
    ARRAY_APPEND(pl_priv(p_playlist)->all_items, p_item);
723

724
    playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
725
    playlist_SendAddNotify( p_playlist, p_item,
726
                            !( i_mode & PLAYLIST_NO_REBUILD ) );
727 728 729
}

/* Actually convert an item to a node */
730
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
731 732
{
    int i;
733 734 735 736 737 738 739 740 741 742
    if( p_item->i_children != -1 ) return;

    p_item->i_children = 0;

    input_item_t *p_input = p_item->p_input;
    vlc_mutex_lock( &p_input->lock );
    p_input->i_type = ITEM_TYPE_NODE;
    vlc_mutex_unlock( &p_input->lock );

    var_SetAddress( p_playlist, "item-change", p_item->p_input );
743 744

    /* Remove it from the array of available items */
Clément Stenac's avatar
Clément Stenac committed
745 746 747
    ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
    if( i != -1 )
        ARRAY_REMOVE( p_playlist->items, i );
748 749
}

750 751 752 753 754
static int RecursiveAddIntoParent (
    playlist_t *p_playlist, playlist_item_t *p_parent,
    input_item_node_t *p_node, int i_pos, bool b_flat,
    playlist_item_t **pp_first_leaf )
{
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    *pp_first_leaf = NULL;

    if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );

    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;

    for( int i = 0; i < p_node->i_children; i++ )
    {
        input_item_node_t *p_child_node = p_node->pp_children[i];

        playlist_item_t *p_new_item = NULL;
        bool b_children = p_child_node->i_children > 0;

        //Create the playlist item represented by input node, if allowed.
        if( !(b_flat && b_children) )
        {
            p_new_item = playlist_NodeAddInput( p_playlist,
772 773 774
                                                p_child_node->p_item,
                                                p_parent,
                                                PLAYLIST_INSERT, i_pos );
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
            if( !p_new_item ) return i_pos;

            i_pos++;
        }
        //Recurse if any children
        if( b_children )
        {
            //Substitute p_new_item for first child leaf
            //(If flat, continue counting from current position)
            int i_last_pos = RecursiveAddIntoParent(
                    p_playlist,
                    p_new_item ? p_new_item : p_parent,
                    p_child_node,
                    ( b_flat ? i_pos : 0 ),
                    b_flat,
                    &p_new_item );
            //If flat, take position after recursion as current position
            if( b_flat ) i_pos = i_last_pos;
        }

        assert( p_new_item != NULL );
        if( i == 0 ) *pp_first_leaf = p_new_item;
    }
    return i_pos;
799
}
800 801 802 803 804 805 806 807 808 809 810 811

static int RecursiveInsertCopy (
    playlist_t *p_playlist, playlist_item_t *p_item,
    playlist_item_t *p_parent, int i_pos, bool b_flat )
{
    PL_ASSERT_LOCKED;
    assert( p_parent != NULL && p_item != NULL );

    if( p_item == p_parent ) return i_pos;

    input_item_t *p_input = p_item->p_input;

812
    if( p_item->i_children == -1 || !b_flat )
813 814
    {
        playlist_item_t *p_new_item = NULL;
815

816
        if( p_item->i_children == -1 )
817 818 819 820 821 822 823
        {
            input_item_t *p_new_input = input_item_Copy( p_input );

            if( likely(p_new_input != NULL) )
            {
                p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
                                                    p_parent, PLAYLIST_INSERT,
824
                                                    i_pos );
825 826 827
                vlc_gc_decref( p_new_input );
            }
        }
828
        else
829 830 831 832 833 834 835 836
        {
            vlc_mutex_lock( &p_input->lock );
            p_new_item = playlist_NodeCreate( p_playlist, p_input->psz_name,
                                              p_parent, i_pos, 0 );
            vlc_mutex_unlock( &p_input->lock );
        }
        if( unlikely(p_new_item == NULL) )
            return i_pos;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

        i_pos++;

        if( p_new_item->i_children != -1 )
            p_parent = p_new_item;
    }

    for( int i = 0; i < p_item->i_children; i++ )
    {
        if( b_flat )
            i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
                                         p_parent, i_pos, true );
        else
            RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
                                 p_parent, p_parent->i_children, false );
    }

    return i_pos;
}