1 /*****************************************************************************
2  * item.c : Playlist item creation/deletion/add/removal functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 VLC authors and VideoLAN
5  * $Id: fcfa8ffadc12d2102c2646b90ad5a5edaa225a05 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include <assert.h>
29 #include <limits.h>
30 #ifdef HAVE_SEARCH_H
31 # include <search.h>
32 #endif
33 
34 #include <vlc_common.h>
35 #include <vlc_playlist.h>
36 #include <vlc_rand.h>
37 #include "playlist_internal.h"
38 #include "libvlc.h"
39 
40 static void playlist_Preparse( playlist_t *, playlist_item_t * );
41 
42 static int RecursiveAddIntoParent (
43                 playlist_t *p_playlist, playlist_item_t *p_parent,
44                 input_item_node_t *p_node, int i_pos, bool b_flat,
45                 playlist_item_t **pp_first_leaf );
46 static int RecursiveInsertCopy (
47                 playlist_t *p_playlist, playlist_item_t *p_item,
48                 playlist_item_t *p_parent, int i_pos, bool b_flat );
49 
50 /*****************************************************************************
51  * An input item has gained subitems (Event Callback)
52  *****************************************************************************/
53 
input_item_add_subitem_tree(const vlc_event_t * p_event,void * user_data)54 static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
55                                           void * user_data )
56 {
57     input_item_t *p_input = p_event->p_obj;
58     playlist_t *p_playlist = user_data;
59     playlist_private_t *p_sys = pl_priv( p_playlist );
60     input_item_node_t *p_new_root = p_event->u.input_item_subitem_tree_added.p_root;
61 
62     PL_LOCK;
63 
64     playlist_item_t *p_item =
65         playlist_ItemGetByInput( p_playlist, p_input );
66 
67     assert( p_item != NULL );
68 
69     bool b_current = get_current_status_item( p_playlist ) == p_item;
70     bool b_autostart = var_GetBool( p_playlist, "playlist-autostart" );
71     bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
72     bool b_flat = false;
73 
74     p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
75 
76     /* We will have to flatten the tree out if we are in "the playlist" node and
77     the user setting demands flat playlist */
78 
79     if( !pl_priv(p_playlist)->b_tree ) {
80         playlist_item_t *p_up = p_item;
81         while( p_up->p_parent )
82         {
83             if( p_up->p_parent == p_playlist->p_playing )
84             {
85                 b_flat = true;
86                 break;
87             }
88             p_up = p_up->p_parent;
89         }
90     }
91 
92     int pos = 0;
93 
94     /* If we have to flatten out, then take the item's position in the parent as
95     insertion point and delete the item */
96 
97     bool b_redirect_request = false;
98 
99     if( b_flat )
100     {
101         playlist_item_t *p_parent = p_item->p_parent;
102         assert( p_parent != NULL );
103 
104         int i;
105         for( i = 0; i < p_parent->i_children; i++ )
106         {
107             if( p_parent->pp_children[i] == p_item )
108             {
109                 pos = i;
110                 break;
111             }
112         }
113         assert( i < p_parent->i_children );
114 
115         playlist_NodeDeleteExplicit( p_playlist, p_item, 0 );
116 
117         /* If there is a pending request referring to the item we just deleted
118          * it needs to be updated so that we do not try to play an entity that
119          * is no longer part of the playlist. */
120 
121         if( p_sys->request.b_request &&
122             ( p_sys->request.p_item == p_item ||
123               p_sys->request.p_node == p_item ) )
124         {
125             b_redirect_request = true;
126         }
127 
128         p_item = p_parent;
129     }
130     else
131     {
132         pos = p_item->i_children >= 0 ? p_item->i_children : 0;
133     }
134 
135     /* At this point:
136     "p_item" is the node where sub-items should be inserted,
137     "pos" is the insertion position in that node */
138 
139     int last_pos = playlist_InsertInputItemTree( p_playlist,
140                                                  p_item,
141                                                  p_new_root,
142                                                  pos,
143                                                  b_flat );
144     if( b_redirect_request )
145     {
146         /* a redirect of the pending request is required, as such we update the
147          * request to refer to the item that would have been the next in line
148          * (if any). */
149 
150         assert( b_flat );
151 
152         playlist_item_t* p_redirect = NULL;
153 
154         if( p_item->i_children > pos )
155             p_redirect = p_item->pp_children[pos];
156 
157         p_sys->request.p_item = p_redirect;
158         p_sys->request.p_node = NULL;
159     }
160 
161     if( !b_flat ) var_SetInteger( p_playlist, "leaf-to-parent", p_item->i_id );
162 
163     //control playback only if it was the current playing item that got subitems
164     if( b_current )
165     {
166         if( ( b_stop && !b_flat ) || !b_autostart )
167         {
168             playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
169         }
170         else if( last_pos != pos ) /* any children? */
171         {
172             /* Continue to play, either random or the first new item */
173             playlist_item_t *p_play_item;
174 
175             if( var_GetBool( p_playlist, "random" ) )
176             {
177                 p_play_item = NULL;
178             }
179             else
180             {
181                 p_play_item = p_item->pp_children[pos];
182                 /* NOTE: this is a work around the general bug:
183                 if node-to-be-played contains sub-nodes, then second instead
184                 of first leaf starts playing (only in case the leafs have just
185                 been instered and playlist has not yet been rebuilt.)
186                 */
187                 while( p_play_item->i_children > 0 )
188                     p_play_item = p_play_item->pp_children[0];
189             }
190 
191             playlist_ViewPlay( p_playlist, NULL, p_play_item );
192         }
193         else if( b_flat && p_playlist->current.i_size > 0 )
194         {
195             /* If the playlist is flat, empty nodes are automatically deleted;
196              * meaning that moving from the current index (the index of a now
197              * removed node) to the next would result in a skip of one entry
198              * (as the empty node is deleted, the logical next item would be
199              * the one that now resides in its place).
200              *
201              * Imagine [ A, B, C, D ], where B (at index 1) is currently being
202              * played and deleted. C is the logically next item, but since the
203              * list now looks like [ A, C, D ], advancing to index 2 would mean
204              * D is played - and not C.
205              *
206              * By positioning the playlist-head at index 0 (A), when the
207              * playlist advance to the next item, C will correctly be played.
208              *
209              * Note: Of course, if the deleted item is at index 0, we should
210              * play whatever item is at position 0 since we cannot advance to
211              * index -1 (as there cannot possibly be any item there).
212              **/
213 
214             if( last_pos )
215                 ResetCurrentlyPlaying( p_playlist,
216                     ARRAY_VAL( p_playlist->current, last_pos - 1 ) );
217             else
218                 playlist_ViewPlay( p_playlist, NULL,
219                     ARRAY_VAL( p_playlist->current, 0 ) );
220         }
221     }
222 
223     PL_UNLOCK;
224 }
225 /*****************************************************************************
226  * An input item's meta or duration has changed (Event Callback)
227  *****************************************************************************/
input_item_changed(const vlc_event_t * p_event,void * user_data)228 static void input_item_changed( const vlc_event_t * p_event,
229                                 void * user_data )
230 {
231     playlist_t *p_playlist = user_data;
232 
233     var_SetAddress( p_playlist, "item-change", p_event->p_obj );
234 }
235 
playlist_ItemCmpId(const void * a,const void * b)236 static int playlist_ItemCmpId( const void *a, const void *b )
237 {
238     const playlist_item_t *pa = a, *pb = b;
239 
240     /* ID are between 1 and INT_MAX, this cannot overflow. */
241     return pa->i_id - pb->i_id;
242 }
243 
playlist_ItemCmpInput(const void * a,const void * b)244 static int playlist_ItemCmpInput( const void *a, const void *b )
245 {
246     const playlist_item_t *pa = a, *pb = b;
247 
248     if( pa->p_input == pb->p_input )
249         return 0;
250     return (((uintptr_t)pa->p_input) > ((uintptr_t)pb->p_input))
251         ? +1 : -1;
252 }
253 
254 /*****************************************************************************
255  * Playlist item creation
256  *****************************************************************************/
playlist_ItemNewFromInput(playlist_t * p_playlist,input_item_t * p_input)257 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
258                                               input_item_t *p_input )
259 {
260     playlist_private_t *p = pl_priv(p_playlist);
261     playlist_item_t **pp, *p_item;
262 
263     p_item = malloc( sizeof( playlist_item_t ) );
264     if( unlikely(p_item == NULL) )
265         return NULL;
266 
267     assert( p_input );
268 
269     p_item->p_input = p_input;
270     p_item->i_id = p->i_last_playlist_id;
271     p_item->p_parent = NULL;
272     p_item->i_children = (p_input->i_type == ITEM_TYPE_NODE) ? 0 : -1;
273     p_item->pp_children = NULL;
274     p_item->i_nb_played = 0;
275     p_item->i_flags = 0;
276 
277     PL_ASSERT_LOCKED;
278 
279     do  /* Find an unused ID for the item */
280     {
281         if( unlikely(p_item->i_id == INT_MAX) )
282             p_item->i_id = 0;
283 
284         p_item->i_id++;
285 
286         if( unlikely(p_item->i_id == p->i_last_playlist_id) )
287             goto error; /* All IDs taken */
288 
289         pp = tsearch( p_item, &p->id_tree, playlist_ItemCmpId );
290         if( unlikely(pp == NULL) )
291             goto error;
292 
293         assert( (*pp)->i_id == p_item->i_id );
294         assert( (*pp) == p_item || (*pp)->p_input != p_input );
295     }
296     while( p_item != *pp );
297 
298     pp = tsearch( p_item, &p->input_tree, playlist_ItemCmpInput );
299     if( unlikely(pp == NULL) )
300     {
301         tdelete( p_item, &p->id_tree, playlist_ItemCmpId );
302         goto error;
303     }
304     /* Same input item cannot be inserted twice. */
305     assert( p_item == *pp );
306 
307     p->i_last_playlist_id = p_item->i_id;
308     input_item_Hold( p_item->p_input );
309 
310     vlc_event_manager_t *p_em = &p_item->p_input->event_manager;
311 
312     vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
313                       input_item_add_subitem_tree, p_playlist );
314     vlc_event_attach( p_em, vlc_InputItemDurationChanged,
315                       input_item_changed, p_playlist );
316     vlc_event_attach( p_em, vlc_InputItemMetaChanged,
317                       input_item_changed, p_playlist );
318     vlc_event_attach( p_em, vlc_InputItemNameChanged,
319                       input_item_changed, p_playlist );
320     vlc_event_attach( p_em, vlc_InputItemInfoChanged,
321                       input_item_changed, p_playlist );
322     vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
323                       input_item_changed, p_playlist );
324 
325     return p_item;
326 
327 error:
328     free( p_item );
329     return NULL;
330 }
331 
332 /***************************************************************************
333  * Playlist item destruction
334  ***************************************************************************/
335 
336 /**
337  * Release an item
338  *
339  * \param p_item item to delete
340 */
playlist_ItemRelease(playlist_t * p_playlist,playlist_item_t * p_item)341 void playlist_ItemRelease( playlist_t *p_playlist, playlist_item_t *p_item )
342 {
343     playlist_private_t *p = pl_priv(p_playlist);
344 
345     PL_ASSERT_LOCKED;
346 
347     vlc_event_manager_t *p_em = &p_item->p_input->event_manager;
348 
349     vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
350                       input_item_add_subitem_tree, p_playlist );
351     vlc_event_detach( p_em, vlc_InputItemMetaChanged,
352                       input_item_changed, p_playlist );
353     vlc_event_detach( p_em, vlc_InputItemDurationChanged,
354                       input_item_changed, p_playlist );
355     vlc_event_detach( p_em, vlc_InputItemNameChanged,
356                       input_item_changed, p_playlist );
357     vlc_event_detach( p_em, vlc_InputItemInfoChanged,
358                       input_item_changed, p_playlist );
359     vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
360                       input_item_changed, p_playlist );
361 
362     input_item_Release( p_item->p_input );
363 
364     tdelete( p_item, &p->input_tree, playlist_ItemCmpInput );
365     tdelete( p_item, &p->id_tree, playlist_ItemCmpId );
366     free( p_item->pp_children );
367     free( p_item );
368 }
369 
370 /**
371  * Finds a playlist item by ID.
372  *
373  * Searches for a playlist item with the given ID.
374  *
375  * \note The playlist must be locked, and the result is only valid until the
376  * playlist is unlocked.
377  *
378  * \warning If an item with the given ID is deleted, it is unlikely but
379  * possible that another item will get the same ID. This can result in
380  * mismatches.
381  * Where holding a reference to an input item is a viable option, then
382  * playlist_ItemGetByInput() should be used instead - to avoid this issue.
383  *
384  * @param p_playlist the playlist
385  * @param id ID to look for
386  * @return the matching item or NULL if not found
387  */
playlist_ItemGetById(playlist_t * p_playlist,int id)388 playlist_item_t *playlist_ItemGetById( playlist_t *p_playlist , int id )
389 {
390     playlist_private_t *p = pl_priv(p_playlist);
391     playlist_item_t key, **pp;
392 
393     PL_ASSERT_LOCKED;
394     key.i_id = id;
395     pp = tfind( &key, &p->id_tree, playlist_ItemCmpId );
396     return (pp != NULL) ? *pp : NULL;
397 }
398 
399 /**
400  * Finds a playlist item by input item.
401  *
402  * Searches for a playlist item for the given input item.
403  *
404  * \note The playlist must be locked, and the result is only valid until the
405  * playlist is unlocked.
406  *
407  * \param p_playlist the playlist
408  * \param item input item to look for
409  * \return the playlist item or NULL on failure
410  */
playlist_ItemGetByInput(playlist_t * p_playlist,const input_item_t * item)411 playlist_item_t *playlist_ItemGetByInput( playlist_t * p_playlist,
412                                           const input_item_t *item )
413 {
414     playlist_private_t *p = pl_priv(p_playlist);
415     playlist_item_t key, **pp;
416 
417     PL_ASSERT_LOCKED;
418     key.p_input = (input_item_t *)item;
419     pp = tfind( &key, &p->input_tree, playlist_ItemCmpInput );
420     return (pp != NULL) ? *pp : NULL;
421 }
422 
423 /**
424  * Clear the playlist
425  *
426  * \param p_playlist playlist object
427  * \param b_locked TRUE if the playlist is locked
428  * \return nothing
429  */
playlist_Clear(playlist_t * p_playlist,bool b_locked)430 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
431 {
432     playlist_item_t *p_root = p_playlist->p_playing;
433 
434     PL_LOCK_IF( !b_locked );
435 
436     for( int i = p_root->i_children - 1; i >= 0 ;i-- )
437         playlist_NodeDelete( p_playlist, p_root->pp_children[i] );
438 
439     PL_UNLOCK_IF( !b_locked );
440 }
441 
442 /***************************************************************************
443  * Playlist item addition
444  ***************************************************************************/
445 /**
446  * Playlist add
447  *
448  * Add an item to the playlist
449  * \param p_playlist the playlist to add into
450  * \param psz_uri the mrl to add to the playlist
451  * \param play_now whether to start playing immediately or not
452  * \return VLC_SUCCESS or a VLC error code
453  */
playlist_Add(playlist_t * p_playlist,const char * psz_uri,bool play_now)454 int playlist_Add( playlist_t *p_playlist, const char *psz_uri, bool play_now )
455 {
456     return playlist_AddExt( p_playlist, psz_uri, NULL, play_now,
457                             0, NULL, 0, true );
458 }
459 
460 /**
461  * Add a MRL into the playlist or the media library, duration and options given
462  *
463  * \param p_playlist the playlist to add into
464  * \param psz_uri the mrl to add to the playlist
465  * \param psz_name a text giving a name or description of this item
466  * \param play_now whether to start playing immediately or not
467  * \param i_options the number of options
468  * \param ppsz_options an array of options
469  * \param i_option_flags options flags
470  * \param b_playlist TRUE for playlist, FALSE for media library
471  * \return VLC_SUCCESS or a VLC error code
472 */
playlist_AddExt(playlist_t * p_playlist,const char * psz_uri,const char * psz_name,bool play_now,int i_options,const char * const * ppsz_options,unsigned i_option_flags,bool b_playlist)473 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
474                      const char *psz_name, bool play_now,
475                      int i_options, const char *const *ppsz_options,
476                      unsigned i_option_flags,
477                      bool b_playlist )
478 {
479     input_item_t *p_input = input_item_New( psz_uri, psz_name );
480     if( !p_input )
481         return VLC_ENOMEM;
482     input_item_AddOptions( p_input, i_options, ppsz_options, i_option_flags );
483     int i_ret = playlist_AddInput( p_playlist, p_input, play_now, b_playlist );
484     input_item_Release( p_input );
485     return i_ret;
486 }
487 
488 /**
489  * Add an input item to the playlist node
490  *
491  * \param p_playlist the playlist to add into
492  * \param p_input the input item to add
493  * \param i_mode the mode used when adding
494  * \param b_playlist TRUE for playlist, FALSE for media library
495  * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
496 */
playlist_AddInput(playlist_t * p_playlist,input_item_t * p_input,bool play_now,bool b_playlist)497 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
498                        bool play_now, bool b_playlist )
499 {
500     PL_LOCK;
501     playlist_item_t *item = b_playlist ? p_playlist->p_playing
502                                        : p_playlist->p_media_library;
503 
504     item = playlist_NodeAddInput( p_playlist, p_input, item, PLAYLIST_END );
505 
506     if( likely(item != NULL) && play_now )
507         playlist_ViewPlay( p_playlist, NULL, item );
508     PL_UNLOCK;
509     return (item != NULL) ? VLC_SUCCESS : VLC_ENOMEM;
510 }
511 
512 /**
513  * Add an input item to a given node
514  *
515  * \param p_playlist the playlist to add into
516  * \param p_input the input item to add
517  * \param p_parent the parent item to add into
518  * \param i_pos the position in the playlist where to add. If this is
519  *        PLAYLIST_END the item will be added at the end of the playlist
520  *        regardless of its size
521  * \return the new playlist item
522  */
playlist_NodeAddInput(playlist_t * p_playlist,input_item_t * p_input,playlist_item_t * p_parent,int i_pos)523 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
524                                          input_item_t *p_input,
525                                          playlist_item_t *p_parent, int i_pos )
526 {
527     PL_ASSERT_LOCKED;
528 
529     assert( p_input );
530     assert( p_parent && p_parent->i_children != -1 );
531 
532     playlist_item_t *p_item = playlist_ItemNewFromInput( p_playlist, p_input );
533     if( unlikely(p_item == NULL) )
534         return NULL;
535 
536     if( p_input->i_type != ITEM_TYPE_NODE )
537         ARRAY_APPEND(p_playlist->items, p_item);
538 
539     playlist_NodeInsert( p_parent, p_item, i_pos );
540     playlist_SendAddNotify( p_playlist, p_item );
541     playlist_Preparse( p_playlist, p_item );
542 
543     return p_item;
544 }
545 
546 /**
547  * Copy an item (and all its children, if any) into another node
548  *
549  * \param p_playlist the playlist to operate on
550  * \param p_item the playlist item to copy
551  * \param p_parent the parent item to copy into
552  * \param i_pos the position in the parent item for the new copy;
553  *              if this is PLAYLIST_END, the copy is appended after all
554  *              parent's children
555  * \return the position in parent item just behind the last new item inserted
556  */
playlist_NodeAddCopy(playlist_t * p_playlist,playlist_item_t * p_item,playlist_item_t * p_parent,int i_pos)557 int playlist_NodeAddCopy( playlist_t *p_playlist, playlist_item_t *p_item,
558     playlist_item_t *p_parent, int i_pos )
559 {
560     PL_ASSERT_LOCKED;
561     assert( p_parent != NULL && p_item != NULL );
562     assert( p_parent->i_children > -1 );
563 
564     if( i_pos == PLAYLIST_END )
565         i_pos = p_parent->i_children;
566 
567     bool b_flat = false;
568 
569     for( playlist_item_t* p_up = p_parent; p_up; p_up = p_up->p_parent )
570     {
571         if( p_up == p_playlist->p_playing && !pl_priv(p_playlist)->b_tree )
572             b_flat = true;
573 
574         if( p_up == p_item )
575             /* TODO: We don't support copying a node into itself (yet),
576             because we insert items as we copy. Instead, we should copy
577             all items first and then insert. */
578             return i_pos;
579     }
580 
581     return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
582 }
583 
584 /**
585  * Insert a tree of input items into a given playlist node
586  *
587  * \param p_playlist the playlist to insert into
588  * \param p_parent the receiving playlist node (can be an item)
589  * \param p_node the root of input item tree,
590           only it's contents will be inserted
591  * \param i_pos the position in the playlist where to insert. If this is
592  *        PLAYLIST_END the items will be added at the end of the playlist
593  *        regardless of its size
594  * \param b_flat TRUE if the new tree contents should be flattened into a list
595  * \return the first new leaf inserted (in playing order)
596  */
playlist_InsertInputItemTree(playlist_t * p_playlist,playlist_item_t * p_parent,input_item_node_t * p_node,int i_pos,bool b_flat)597 int playlist_InsertInputItemTree (
598     playlist_t *p_playlist, playlist_item_t *p_parent,
599     input_item_node_t *p_node, int i_pos, bool b_flat )
600 {
601     return RecursiveAddIntoParent( p_playlist, p_parent, p_node, i_pos, b_flat,
602                                    &(playlist_item_t*){ NULL } );
603 }
604 
605 
606 /*****************************************************************************
607  * Playlist item misc operations
608  *****************************************************************************/
609 
ItemIndex(playlist_item_t * p_item)610 static int ItemIndex ( playlist_item_t *p_item )
611 {
612     int idx;
613 
614     TAB_FIND( p_item->p_parent->i_children,
615               p_item->p_parent->pp_children,
616               p_item,
617               idx );
618 
619     return idx;
620 }
621 
622 /**
623  * Moves an item
624  *
625  * This function must be entered with the playlist lock
626  *
627  * \param p_playlist the playlist
628  * \param p_item the item to move
629  * \param p_node the new parent of the item
630  * \param i_newpos the new position under this new parent
631  * \return VLC_SUCCESS or an error
632  */
playlist_TreeMove(playlist_t * p_playlist,playlist_item_t * p_item,playlist_item_t * p_node,int i_newpos)633 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
634                        playlist_item_t *p_node, int i_newpos )
635 {
636     PL_ASSERT_LOCKED;
637 
638     if( p_node->i_children == -1 ) return VLC_EGENERIC;
639 
640     playlist_item_t *p_detach = p_item->p_parent;
641     int i_index = ItemIndex( p_item );
642 
643     TAB_ERASE(p_detach->i_children, p_detach->pp_children, i_index);
644 
645     if( p_detach == p_node && i_index < i_newpos )
646         i_newpos--;
647 
648     TAB_INSERT(p_node->i_children, p_node->pp_children, p_item, i_newpos);
649     p_item->p_parent = p_node;
650 
651     pl_priv( p_playlist )->b_reset_currently_playing = true;
652     vlc_cond_signal( &pl_priv( p_playlist )->signal );
653     return VLC_SUCCESS;
654 }
655 
656 /**
657  * Moves an array of items
658  *
659  * This function must be entered with the playlist lock
660  *
661  * \param p_playlist the playlist
662  * \param i_items the number of indexes to move
663  * \param pp_items the array of indexes to move
664  * \param p_node the target node
665  * \param i_newpos the target position under this node
666  * \return VLC_SUCCESS or an error
667  */
playlist_TreeMoveMany(playlist_t * p_playlist,int i_items,playlist_item_t ** pp_items,playlist_item_t * p_node,int i_newpos)668 int playlist_TreeMoveMany( playlist_t *p_playlist,
669                             int i_items, playlist_item_t **pp_items,
670                             playlist_item_t *p_node, int i_newpos )
671 {
672     PL_ASSERT_LOCKED;
673 
674     if ( p_node->i_children == -1 ) return VLC_EGENERIC;
675 
676     for( int i = 0; i < i_items; i++ )
677     {
678         playlist_item_t *p_item = pp_items[i];
679         int i_index = ItemIndex( p_item );
680         playlist_item_t *p_parent = p_item->p_parent;
681         TAB_ERASE(p_parent->i_children, p_parent->pp_children, i_index);
682         if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
683     }
684     for( int i = i_items - 1; i >= 0; i-- )
685     {
686         playlist_item_t *p_item = pp_items[i];
687         TAB_INSERT(p_node->i_children, p_node->pp_children, p_item, i_newpos);
688         p_item->p_parent = p_node;
689     }
690 
691     pl_priv( p_playlist )->b_reset_currently_playing = true;
692     vlc_cond_signal( &pl_priv( p_playlist )->signal );
693     return VLC_SUCCESS;
694 }
695 
696 /**
697  * Send a notification that an item has been added to a node
698  *
699  * \param p_playlist the playlist object
700  * \param i_item_id id of the item added
701  * \param i_node_id id of the node in which the item was added
702  * \return nothing
703  */
playlist_SendAddNotify(playlist_t * p_playlist,playlist_item_t * item)704 void playlist_SendAddNotify( playlist_t *p_playlist, playlist_item_t *item )
705 {
706     playlist_private_t *p_sys = pl_priv(p_playlist);
707     PL_ASSERT_LOCKED;
708 
709     p_sys->b_reset_currently_playing = true;
710     vlc_cond_signal( &p_sys->signal );
711 
712     var_SetAddress( p_playlist, "playlist-item-append", item );
713 }
714 
715 /**
716  * Get the duration of all items in a node.
717  */
playlist_GetNodeDuration(playlist_item_t * node)718 mtime_t playlist_GetNodeDuration( playlist_item_t* node )
719 {
720     mtime_t duration = input_item_GetDuration( node->p_input );
721     if( duration == -1 )
722         duration = 0;
723 
724     for( int i = 0; i < node->i_children; i++ )
725         duration += playlist_GetNodeDuration( node->pp_children[i] );
726 
727     return duration;
728 }
729 
730 /***************************************************************************
731  * The following functions are local
732  ***************************************************************************/
733 
734 /* Enqueue an item for preparsing */
playlist_Preparse(playlist_t * p_playlist,playlist_item_t * p_item)735 static void playlist_Preparse( playlist_t *p_playlist,
736                                playlist_item_t *p_item )
737 {
738     playlist_private_t *sys = pl_priv(p_playlist);
739     input_item_t *input = p_item->p_input;
740 
741     PL_ASSERT_LOCKED;
742     /* Preparse if no artist/album info, and hasn't been preparsed already
743        and if user has some preparsing option (auto-preparse variable)
744        enabled*/
745     char *psz_artist = input_item_GetArtist( input );
746     char *psz_album = input_item_GetAlbum( input );
747 
748     if( sys->b_preparse && !input_item_IsPreparsed( input )
749      && (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album)) )
750         vlc_MetadataRequest( p_playlist->obj.libvlc, input, 0, -1, p_item );
751     free( psz_artist );
752     free( psz_album );
753 }
754 
755 /* Actually convert an item to a node */
ChangeToNode(playlist_t * p_playlist,playlist_item_t * p_item)756 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
757 {
758     int i;
759     if( p_item->i_children != -1 ) return;
760 
761     p_item->i_children = 0;
762 
763     input_item_t *p_input = p_item->p_input;
764     vlc_mutex_lock( &p_input->lock );
765     p_input->i_type = ITEM_TYPE_NODE;
766     vlc_mutex_unlock( &p_input->lock );
767 
768     var_SetAddress( p_playlist, "item-change", p_item->p_input );
769 
770     /* Remove it from the array of available items */
771     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
772     if( i != -1 )
773         ARRAY_REMOVE( p_playlist->items, i );
774 }
775 
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)776 static int RecursiveAddIntoParent (
777     playlist_t *p_playlist, playlist_item_t *p_parent,
778     input_item_node_t *p_node, int i_pos, bool b_flat,
779     playlist_item_t **pp_first_leaf )
780 {
781     *pp_first_leaf = NULL;
782 
783     if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
784 
785     if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
786 
787     for( int i = 0; i < p_node->i_children; i++ )
788     {
789         input_item_node_t *p_child_node = p_node->pp_children[i];
790 
791         playlist_item_t *p_new_item = NULL;
792         bool b_children = p_child_node->i_children > 0;
793 
794         //Create the playlist item represented by input node, if allowed.
795         if( !(b_flat && b_children) )
796         {
797             p_new_item = playlist_NodeAddInput( p_playlist,
798                                                 p_child_node->p_item,
799                                                 p_parent, i_pos );
800             if( !p_new_item ) return i_pos;
801 
802             i_pos++;
803         }
804         //Recurse if any children
805         if( b_children )
806         {
807             //Substitute p_new_item for first child leaf
808             //(If flat, continue counting from current position)
809             int i_last_pos = RecursiveAddIntoParent(
810                     p_playlist,
811                     p_new_item ? p_new_item : p_parent,
812                     p_child_node,
813                     ( b_flat ? i_pos : 0 ),
814                     b_flat,
815                     &p_new_item );
816             //If flat, take position after recursion as current position
817             if( b_flat ) i_pos = i_last_pos;
818         }
819 
820         assert( p_new_item != NULL );
821         if( i == 0 ) *pp_first_leaf = p_new_item;
822     }
823     return i_pos;
824 }
825 
RecursiveInsertCopy(playlist_t * p_playlist,playlist_item_t * p_item,playlist_item_t * p_parent,int i_pos,bool b_flat)826 static int RecursiveInsertCopy (
827     playlist_t *p_playlist, playlist_item_t *p_item,
828     playlist_item_t *p_parent, int i_pos, bool b_flat )
829 {
830     PL_ASSERT_LOCKED;
831     assert( p_parent != NULL && p_item != NULL );
832 
833     if( p_item == p_parent ) return i_pos;
834 
835     input_item_t *p_input = p_item->p_input;
836 
837     if( p_item->i_children == -1 || !b_flat )
838     {
839         playlist_item_t *p_new_item = NULL;
840 
841         if( p_item->i_children == -1 )
842         {
843             input_item_t *p_new_input = input_item_Copy( p_input );
844 
845             if( likely(p_new_input != NULL) )
846             {
847                 p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
848                                                     p_parent, i_pos );
849                 input_item_Release( p_new_input );
850             }
851         }
852         else
853         {
854             vlc_mutex_lock( &p_input->lock );
855             p_new_item = playlist_NodeCreate( p_playlist, p_input->psz_name,
856                                               p_parent, i_pos, 0 );
857             vlc_mutex_unlock( &p_input->lock );
858         }
859         if( unlikely(p_new_item == NULL) )
860             return i_pos;
861 
862         i_pos++;
863 
864         if( p_new_item->i_children != -1 )
865             p_parent = p_new_item;
866     }
867 
868     for( int i = 0; i < p_item->i_children; i++ )
869     {
870         if( b_flat )
871             i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
872                                          p_parent, i_pos, true );
873         else
874             RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
875                                  p_parent, p_parent->i_children, false );
876     }
877 
878     return i_pos;
879 }
880