]> git.sesse.net Git - vlc/blob - src/playlist/item.c
mediacodec: don't loop in GetOutput
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item creation/deletion/add/removal functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 VLC authors and VideoLAN
5  * $Id$
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 <vlc_common.h>
29 #include <assert.h>
30 #include <vlc_playlist.h>
31 #include <vlc_rand.h>
32 #include "playlist_internal.h"
33
34 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
35                      playlist_item_t *p_node, int i_mode, int i_pos );
36 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 static int RecursiveAddIntoParent (
41                 playlist_t *p_playlist, playlist_item_t *p_parent,
42                 input_item_node_t *p_node, int i_pos, bool b_flat,
43                 playlist_item_t **pp_first_leaf );
44 static int RecursiveInsertCopy (
45                 playlist_t *p_playlist, playlist_item_t *p_item,
46                 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 static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
53                                           void * user_data )
54 {
55     input_item_t *p_input = p_event->p_obj;
56     playlist_t *p_playlist = (( playlist_item_t* ) user_data)->p_playlist;
57     input_item_node_t *p_new_root = p_event->u.input_item_subitem_tree_added.p_root;
58
59     PL_LOCK;
60
61     playlist_item_t *p_item =
62         playlist_ItemGetByInput( p_playlist, p_input );
63
64     assert( p_item != NULL );
65
66     bool b_current = get_current_status_item( p_playlist ) == p_item;
67     bool b_autostart = var_GetBool( p_playlist, "playlist-autostart" );
68     bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
69     bool b_flat = false;
70
71     p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
72
73     /* We will have to flatten the tree out if we are in "the playlist" node and
74     the user setting demands flat playlist */
75
76     if( !pl_priv(p_playlist)->b_tree ) {
77         playlist_item_t *p_up = p_item;
78         while( p_up->p_parent )
79         {
80             if( p_up->p_parent == p_playlist->p_playing )
81             {
82                 b_flat = true;
83                 break;
84             }
85             p_up = p_up->p_parent;
86         }
87     }
88
89     int pos = 0;
90
91     /* If we have to flatten out, then take the item's position in the parent as
92     insertion point and delete the item */
93
94     if( b_flat )
95     {
96         playlist_item_t *p_parent = p_item->p_parent;
97         assert( p_parent != NULL );
98
99         int i;
100         for( i = 0; i < p_parent->i_children; i++ )
101         {
102             if( p_parent->pp_children[i] == p_item )
103             {
104                 pos = i;
105                 break;
106             }
107         }
108         assert( i < p_parent->i_children );
109
110         playlist_DeleteItem( p_playlist, p_item, true );
111
112         p_item = p_parent;
113     }
114     else
115     {
116         pos = p_item->i_children >= 0 ? p_item->i_children : 0;
117     }
118
119     /* At this point:
120     "p_item" is the node where sub-items should be inserted,
121     "pos" is the insertion position in that node */
122
123     int last_pos = playlist_InsertInputItemTree( p_playlist,
124                                                  p_item,
125                                                  p_new_root,
126                                                  pos,
127                                                  b_flat );
128
129     if( !b_flat ) var_SetInteger( p_playlist, "leaf-to-parent", p_item->i_id );
130
131     //control playback only if it was the current playing item that got subitems
132     if( b_current )
133     {
134         if( last_pos == pos || ( b_stop && !b_flat ) || !b_autostart )
135         {
136             /* We stop, either because no sub-item was actually created, or some
137             flags/settings want us to do so at this point */
138             PL_UNLOCK;
139             playlist_Stop( p_playlist );
140             return;
141         }
142         else
143         {
144             /* Continue to play, either random or the first new item */
145             playlist_item_t *p_play_item;
146
147             if( var_GetBool( p_playlist, "random" ) )
148             {
149                 p_play_item = NULL;
150             }
151             else
152             {
153                 p_play_item = p_item->pp_children[pos];
154                 /* NOTE: this is a work around the general bug:
155                 if node-to-be-played contains sub-nodes, then second instead
156                 of first leaf starts playing (only in case the leafs have just
157                 been instered and playlist has not yet been rebuilt.)
158                 */
159                 while( p_play_item->i_children > 0 )
160                     p_play_item = p_play_item->pp_children[0];
161             }
162
163             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
164                                   pl_Locked,
165                                   get_current_status_node( p_playlist ),
166                                   p_play_item );
167         }
168     }
169
170     PL_UNLOCK;
171 }
172 /*****************************************************************************
173  * An input item's meta or duration has changed (Event Callback)
174  *****************************************************************************/
175 static void input_item_changed( const vlc_event_t * p_event,
176                                 void * user_data )
177 {
178     playlist_item_t *p_item = user_data;
179     VLC_UNUSED( p_event );
180     var_SetAddress( p_item->p_playlist, "item-change", p_item->p_input );
181 }
182
183 /*****************************************************************************
184  * Listen to vlc_InputItemAddSubItem event
185  *****************************************************************************/
186 static void install_input_item_observer( playlist_item_t * p_item )
187 {
188     vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
189     vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
190                       input_item_add_subitem_tree, p_item );
191     vlc_event_attach( p_em, vlc_InputItemDurationChanged,
192                       input_item_changed, p_item );
193     vlc_event_attach( p_em, vlc_InputItemMetaChanged,
194                       input_item_changed, p_item );
195     vlc_event_attach( p_em, vlc_InputItemNameChanged,
196                       input_item_changed, p_item );
197     vlc_event_attach( p_em, vlc_InputItemInfoChanged,
198                       input_item_changed, p_item );
199     vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
200                       input_item_changed, p_item );
201 }
202
203 static void uninstall_input_item_observer( playlist_item_t * p_item )
204 {
205     vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
206     vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
207                       input_item_add_subitem_tree, p_item );
208     vlc_event_detach( p_em, vlc_InputItemMetaChanged,
209                       input_item_changed, p_item );
210     vlc_event_detach( p_em, vlc_InputItemDurationChanged,
211                       input_item_changed, p_item );
212     vlc_event_detach( p_em, vlc_InputItemNameChanged,
213                       input_item_changed, p_item );
214     vlc_event_detach( p_em, vlc_InputItemInfoChanged,
215                       input_item_changed, p_item );
216     vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
217                       input_item_changed, p_item );
218 }
219
220 /*****************************************************************************
221  * Playlist item creation
222  *****************************************************************************/
223 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
224                                               input_item_t *p_input )
225 {
226     playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
227     if( !p_item )
228         return NULL;
229
230     assert( p_input );
231
232     p_item->p_input = p_input;
233     vlc_gc_incref( p_item->p_input );
234
235     p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
236
237     p_item->p_parent = NULL;
238     p_item->i_children = -1;
239     p_item->pp_children = NULL;
240     p_item->i_nb_played = 0;
241     p_item->i_flags = 0;
242     p_item->p_playlist = p_playlist;
243
244     install_input_item_observer( p_item );
245
246     return p_item;
247 }
248
249 /***************************************************************************
250  * Playlist item destruction
251  ***************************************************************************/
252
253 /**
254  * Release an item
255  *
256  * \param p_item item to delete
257  * \return VLC_SUCCESS
258 */
259 int playlist_ItemRelease( playlist_item_t *p_item )
260 {
261     /* For the assert */
262     playlist_t *p_playlist = p_item->p_playlist;
263     PL_ASSERT_LOCKED;
264
265     /* Surprise, we can't actually do more because we
266      * don't do refcounting, or eauivalent.
267      * Because item are not only accessed by their id
268      * using playlist_item outside the PL_LOCK isn't safe.
269      * Most of the modules does that.
270      *
271      * Who wants to add proper memory management? */
272     uninstall_input_item_observer( p_item );
273     ARRAY_APPEND( pl_priv(p_playlist)->items_to_delete, p_item);
274     return VLC_SUCCESS;
275 }
276
277 /**
278  * Delete input item
279  *
280  * Remove an input item when it appears from a root playlist item
281  * \param p_playlist playlist object
282  * \param p_input the input to delete
283  * \param p_root root playlist item
284  * \param b_do_stop must stop or not the playlist
285  * \return VLC_SUCCESS or VLC_EGENERIC
286 */
287 static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
288                             playlist_item_t *p_root, bool b_do_stop )
289 {
290     PL_ASSERT_LOCKED;
291     playlist_item_t *p_item = playlist_ItemFindFromInputAndRoot(
292         p_playlist, p_input, p_root, false );
293     if( !p_item ) return VLC_EGENERIC;
294     return playlist_DeleteItem( p_playlist, p_item, b_do_stop );
295 }
296
297 /**
298  * Delete input item
299  *
300  * Remove an input item when it appears from a root playlist item
301  * \param p_playlist playlist object
302  * \param p_input the input to delete
303  * \param p_root root playlist item
304  * \param b_locked TRUE if the playlist is locked
305  * \return VLC_SUCCESS or VLC_EGENERIC
306  */
307 int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
308                                       input_item_t *p_item,
309                                       playlist_item_t *p_root, bool b_locked )
310 {
311     int i_ret;
312     PL_LOCK_IF( !b_locked );
313     i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
314     PL_UNLOCK_IF( !b_locked );
315     return i_ret;
316 }
317
318 /**
319  * Delete from input
320  *
321  * Search anywhere in playlist for an an input item and delete it
322  * \param p_playlist playlist object
323  * \param p_input the input to delete
324  * \param b_locked TRUE if the playlist is locked
325  * \return VLC_SUCCESS or VLC_ENOITEM
326  */
327 int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
328                               bool b_locked )
329 {
330     int i_ret;
331     PL_LOCK_IF( !b_locked );
332     i_ret = DeleteFromInput( p_playlist, p_input,
333                              p_playlist->p_root, true );
334     PL_UNLOCK_IF( !b_locked );
335     return ( i_ret == VLC_SUCCESS ? VLC_SUCCESS : VLC_ENOITEM );
336 }
337
338 /**
339  * Clear the playlist
340  *
341  * \param p_playlist playlist object
342  * \param b_locked TRUE if the playlist is locked
343  * \return nothing
344  */
345 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
346 {
347     PL_LOCK_IF( !b_locked );
348     playlist_NodeEmpty( p_playlist, p_playlist->p_playing, true );
349     PL_UNLOCK_IF( !b_locked );
350 }
351
352 /**
353  * Delete playlist item
354  *
355  * Remove a playlist item from the playlist, given its id
356  * This function is to be used only by the playlist
357  * \param p_playlist playlist object
358  * \param i_id id of the item do delete
359  * \return VLC_SUCCESS or an error
360  */
361 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
362 {
363     PL_ASSERT_LOCKED;
364     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
365     if( !p_item ) return VLC_EGENERIC;
366     return playlist_DeleteItem( p_playlist, p_item, true );
367 }
368
369 /***************************************************************************
370  * Playlist item addition
371  ***************************************************************************/
372 /**
373  * Playlist add
374  *
375  * Add an item to the playlist or the media library
376  * \param p_playlist the playlist to add into
377  * \param psz_uri the mrl to add to the playlist
378  * \param psz_name a text giving a name or description of this item
379  * \param i_mode the mode used when adding
380  * \param i_pos the position in the playlist where to add. If this is
381  *        PLAYLIST_END the item will be added at the end of the playlist
382  *        regardless of its size
383  * \param b_playlist TRUE for playlist, FALSE for media library
384  * \param b_locked TRUE if the playlist is locked
385  * \return VLC_SUCCESS or a VLC error code
386  */
387 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
388                   const char *psz_name, int i_mode, int i_pos,
389                   bool b_playlist, bool b_locked )
390 {
391     return playlist_AddExt( p_playlist, psz_uri, psz_name,
392                             i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
393 }
394
395 /**
396  * Add a MRL into the playlist or the media library, duration and options given
397  *
398  * \param p_playlist the playlist to add into
399  * \param psz_uri the mrl to add to the playlist
400  * \param psz_name a text giving a name or description of this item
401  * \param i_mode the mode used when adding
402  * \param i_pos the position in the playlist where to add. If this is
403  *        PLAYLIST_END the item will be added at the end of the playlist
404  *        regardless of its size
405  * \param i_duration length of the item in milliseconds.
406  * \param i_options the number of options
407  * \param ppsz_options an array of options
408  * \param i_option_flags options flags
409  * \param b_playlist TRUE for playlist, FALSE for media library
410  * \param b_locked TRUE if the playlist is locked
411  * \return VLC_SUCCESS or a VLC error code
412 */
413 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
414                      const char *psz_name, int i_mode, int i_pos,
415                      mtime_t i_duration,
416                      int i_options, const char *const *ppsz_options,
417                      unsigned i_option_flags,
418                      bool b_playlist, bool b_locked )
419 {
420     int i_ret;
421     input_item_t *p_input;
422
423     p_input = input_item_NewExt( psz_uri, psz_name,
424                                  i_options, ppsz_options, i_option_flags,
425                                  i_duration );
426     if( p_input == NULL )
427         return VLC_ENOMEM;
428     i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
429                                b_locked );
430     vlc_gc_decref( p_input );
431     return i_ret;
432 }
433
434 /**
435  * Add an input item to the playlist node
436  *
437  * \param p_playlist the playlist to add into
438  * \param p_input the input item to add
439  * \param i_mode the mode used when adding
440  * \param i_pos the position in the playlist where to add. If this is
441  *        PLAYLIST_END the item will be added at the end of the playlist
442  *        regardless of its size
443  * \param b_playlist TRUE for playlist, FALSE for media library
444  * \param b_locked TRUE if the playlist is locked
445  * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
446 */
447 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
448                        int i_mode, int i_pos, bool b_playlist,
449                        bool b_locked )
450 {
451     playlist_item_t *p_item;
452
453     PL_LOCK_IF( !b_locked );
454
455     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
456     if( p_item == NULL )
457     {
458         PL_UNLOCK_IF( !b_locked );
459         return VLC_ENOMEM;
460     }
461     AddItem( p_playlist, p_item,
462              b_playlist ? p_playlist->p_playing :
463                           p_playlist->p_media_library , i_mode, i_pos );
464
465     GoAndPreparse( p_playlist, i_mode, p_item );
466
467     PL_UNLOCK_IF( !b_locked );
468     return VLC_SUCCESS;
469 }
470
471 /**
472  * Add an input item to a given node
473  *
474  * \param p_playlist the playlist to add into
475  * \param p_input the input item to add
476  * \param p_parent the parent item to add into
477  * \param i_mode the mode used when addin
478  * \param i_pos the position in the playlist where to add. If this is
479  *        PLAYLIST_END the item will be added at the end of the playlist
480  *        regardless of its size
481  * \param b_locked TRUE if the playlist is locked
482  * \return the new playlist item
483  */
484 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
485                                          input_item_t *p_input,
486                                          playlist_item_t *p_parent,
487                                          int i_mode, int i_pos,
488                                          bool b_locked )
489 {
490     playlist_item_t *p_item;
491     assert( p_input );
492     assert( p_parent && p_parent->i_children != -1 );
493
494     PL_LOCK_IF( !b_locked );
495
496     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
497     if( p_item == NULL )
498         goto end;
499     AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
500
501     GoAndPreparse( p_playlist, i_mode, p_item );
502
503 end:
504     PL_UNLOCK_IF( !b_locked );
505
506     return p_item;
507 }
508
509 /**
510  * Copy an item (and all its children, if any) into another node
511  *
512  * \param p_playlist the playlist to operate on
513  * \param p_item the playlist item to copy
514  * \param p_parent the parent item to copy into
515  * \param i_pos the position in the parent item for the new copy;
516  *              if this is PLAYLIST_END, the copy is appended after all
517  *              parent's children
518  * \return the position in parent item just behind the last new item inserted
519  */
520 int playlist_NodeAddCopy (
521     playlist_t *p_playlist, playlist_item_t *p_item,
522     playlist_item_t *p_parent, int i_pos )
523 {
524     PL_ASSERT_LOCKED;
525     assert( p_parent != NULL && p_item != NULL );
526     assert( p_parent->i_children > -1 );
527
528     if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
529
530     bool b_flat = false;
531
532     playlist_item_t *p_up = p_parent;
533     while( p_up )
534     {
535         if( p_up == p_playlist->p_playing )
536             if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
537         if( p_up == p_item )
538             /* TODO: We don't support copying a node into itself (yet),
539             because we insert items as we copy. Instead, we should copy
540             all items first and then insert. */
541             return i_pos;
542         p_up = p_up->p_parent;
543     }
544
545     return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
546 }
547
548 /**
549  * Insert a tree of input items into a given playlist node
550  *
551  * \param p_playlist the playlist to insert into
552  * \param p_parent the receiving playlist node (can be an item)
553  * \param p_node the root of input item tree,
554           only it's contents will be inserted
555  * \param i_pos the position in the playlist where to insert. If this is
556  *        PLAYLIST_END the items will be added at the end of the playlist
557  *        regardless of its size
558  * \param b_flat TRUE if the new tree contents should be flattened into a list
559  * \return the first new leaf inserted (in playing order)
560  */
561 int playlist_InsertInputItemTree (
562     playlist_t *p_playlist, playlist_item_t *p_parent,
563     input_item_node_t *p_node, int i_pos, bool b_flat )
564 {
565     playlist_item_t *p_first_leaf = NULL;
566     return RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
567 }
568
569
570 /*****************************************************************************
571  * Playlist item misc operations
572  *****************************************************************************/
573
574 /**
575  * Find an item within a root, given its input id.
576  *
577  * \param p_playlist the playlist object
578  * \param p_item the input item
579  * \param p_root root playlist item
580  * \param b_items_only TRUE if we want the item himself
581  * \return the first found item, or NULL if not found
582  */
583 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
584                                                     input_item_t *p_item,
585                                                     playlist_item_t *p_root,
586                                                     bool b_items_only )
587 {
588     int i;
589     for( i = 0 ; i< p_root->i_children ; i++ )
590     {
591         if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
592             p_root->pp_children[i]->p_input == p_item )
593         {
594             return p_root->pp_children[i];
595         }
596         else if( p_root->pp_children[i]->i_children >= 0 )
597         {
598             playlist_item_t *p_search =
599                  playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
600                                                     p_root->pp_children[i],
601                                                     b_items_only );
602             if( p_search ) return p_search;
603         }
604     }
605     return NULL;
606 }
607
608
609 static int ItemIndex ( playlist_item_t *p_item )
610 {
611     for( int i = 0; i < p_item->p_parent->i_children; i++ )
612         if( p_item->p_parent->pp_children[i] == p_item ) return i;
613     return -1;
614 }
615
616 /**
617  * Moves an item
618  *
619  * This function must be entered with the playlist lock
620  *
621  * \param p_playlist the playlist
622  * \param p_item the item to move
623  * \param p_node the new parent of the item
624  * \param i_newpos the new position under this new parent
625  * \return VLC_SUCCESS or an error
626  */
627 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
628                        playlist_item_t *p_node, int i_newpos )
629 {
630     PL_ASSERT_LOCKED;
631
632     if( p_node->i_children == -1 ) return VLC_EGENERIC;
633
634     playlist_item_t *p_detach = p_item->p_parent;
635     int i_index = ItemIndex( p_item );
636
637     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );
638
639     if( p_detach == p_node && i_index < i_newpos )
640         i_newpos--;
641
642     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
643     p_item->p_parent = p_node;
644
645     pl_priv( p_playlist )->b_reset_currently_playing = true;
646     vlc_cond_signal( &pl_priv( p_playlist )->signal );
647     return VLC_SUCCESS;
648 }
649
650 /**
651  * Moves an array of items
652  *
653  * This function must be entered with the playlist lock
654  *
655  * \param p_playlist the playlist
656  * \param i_items the number of indexes to move
657  * \param pp_items the array of indexes to move
658  * \param p_node the target node
659  * \param i_newpos the target position under this node
660  * \return VLC_SUCCESS or an error
661  */
662 int playlist_TreeMoveMany( playlist_t *p_playlist,
663                             int i_items, playlist_item_t **pp_items,
664                             playlist_item_t *p_node, int i_newpos )
665 {
666     PL_ASSERT_LOCKED;
667
668     if ( p_node->i_children == -1 ) return VLC_EGENERIC;
669
670     int i;
671     for( i = 0; i < i_items; i++ )
672     {
673         playlist_item_t *p_item = pp_items[i];
674         int i_index = ItemIndex( p_item );
675         playlist_item_t *p_parent = p_item->p_parent;
676         REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
677         if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
678     }
679     for( i = i_items - 1; i >= 0; i-- )
680     {
681         playlist_item_t *p_item = pp_items[i];
682         INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
683         p_item->p_parent = p_node;
684     }
685
686     pl_priv( p_playlist )->b_reset_currently_playing = true;
687     vlc_cond_signal( &pl_priv( p_playlist )->signal );
688     return VLC_SUCCESS;
689 }
690
691 /**
692  * Send a notification that an item has been added to a node
693  *
694  * \param p_playlist the playlist object
695  * \param i_item_id id of the item added
696  * \param i_node_id id of the node in wich the item was added
697  * \param b_signal TRUE if the function must send a signal
698  * \return nothing
699  */
700 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
701                              int i_node_id, bool b_signal )
702 {
703     playlist_private_t *p_sys = pl_priv(p_playlist);
704     PL_ASSERT_LOCKED;
705
706     p_sys->b_reset_currently_playing = true;
707     if( b_signal )
708         vlc_cond_signal( &p_sys->signal );
709
710     playlist_add_t add;
711     add.i_item = i_item_id;
712     add.i_node = i_node_id;
713
714     var_SetAddress( p_playlist, "playlist-item-append", &add );
715 }
716
717 /**
718  * Get the duration of all items in a node.
719  */
720 mtime_t playlist_GetNodeDuration( playlist_item_t* node )
721 {
722     /* For the assert */
723     playlist_t *p_playlist = node->p_playlist;
724     PL_ASSERT_LOCKED;
725
726     mtime_t mt_duration = 0;
727
728     if( node->i_children != -1 )
729         for( int i = 0; i < node->i_children; i++ )
730         {
731             input_item_t* p_input = node->pp_children[i]->p_input;
732             if ( p_input->i_type == ITEM_TYPE_NODE )
733                 mt_duration += playlist_GetNodeDuration( node->pp_children[i] );
734             else
735                 mt_duration += input_item_GetDuration( p_input );
736         }
737
738     return mt_duration;
739 }
740
741 /***************************************************************************
742  * The following functions are local
743  ***************************************************************************/
744
745 /* Enqueue an item for preparsing, and play it, if needed */
746 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
747                            playlist_item_t *p_item )
748 {
749     playlist_private_t *sys = pl_priv(p_playlist);
750
751     PL_ASSERT_LOCKED;
752     if( (i_mode & PLAYLIST_GO ) )
753     {
754         sys->request.b_request = true;
755         sys->request.i_skip = 0;
756         sys->request.p_item = p_item;
757         if( sys->p_input != NULL )
758             input_Stop( sys->p_input );
759         vlc_cond_signal( &sys->signal );
760     }
761     /* Preparse if no artist/album info, and hasn't been preparsed allready
762        and if user has some preparsing option (auto-preparse variable)
763        enabled*/
764     char *psz_artist = input_item_GetArtist( p_item->p_input );
765     char *psz_album = input_item_GetAlbum( p_item->p_input );
766     if( sys->p_preparser != NULL && !input_item_IsPreparsed( p_item->p_input )
767      && (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album)) )
768         playlist_preparser_Push( sys->p_preparser, p_item->p_input, 0 );
769     free( psz_artist );
770     free( psz_album );
771 }
772
773 /* Add the playlist item to the requested node and fire a notification */
774 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
775                      playlist_item_t *p_node, int i_mode, int i_pos )
776 {
777     PL_ASSERT_LOCKED;
778     ARRAY_APPEND(p_playlist->items, p_item);
779     ARRAY_APPEND(p_playlist->all_items, p_item);
780
781     if( i_pos == PLAYLIST_END )
782         playlist_NodeAppend( p_playlist, p_item, p_node );
783     else
784         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
785
786     playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
787                             !( i_mode & PLAYLIST_NO_REBUILD ) );
788 }
789
790 /* Actually convert an item to a node */
791 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
792 {
793     int i;
794     if( p_item->i_children != -1 ) return;
795
796     p_item->i_children = 0;
797
798     input_item_t *p_input = p_item->p_input;
799     vlc_mutex_lock( &p_input->lock );
800     p_input->i_type = ITEM_TYPE_NODE;
801     vlc_mutex_unlock( &p_input->lock );
802
803     var_SetAddress( p_playlist, "item-change", p_item->p_input );
804
805     /* Remove it from the array of available items */
806     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
807     if( i != -1 )
808         ARRAY_REMOVE( p_playlist->items, i );
809 }
810
811 /* Do the actual removal */
812 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
813                         bool b_stop )
814 {
815     assert( b_stop );
816     return playlist_NodeDelete( p_playlist, p_item, true, false );
817 }
818
819 static int RecursiveAddIntoParent (
820     playlist_t *p_playlist, playlist_item_t *p_parent,
821     input_item_node_t *p_node, int i_pos, bool b_flat,
822     playlist_item_t **pp_first_leaf )
823 {
824     *pp_first_leaf = NULL;
825
826     if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
827
828     if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
829
830     for( int i = 0; i < p_node->i_children; i++ )
831     {
832         input_item_node_t *p_child_node = p_node->pp_children[i];
833
834         playlist_item_t *p_new_item = NULL;
835         bool b_children = p_child_node->i_children > 0;
836
837         //Create the playlist item represented by input node, if allowed.
838         if( !(b_flat && b_children) )
839         {
840             p_new_item = playlist_NodeAddInput( p_playlist,
841                     p_child_node->p_item,
842                     p_parent,
843                     PLAYLIST_INSERT, i_pos,
844                     pl_Locked );
845             if( !p_new_item ) return i_pos;
846
847             i_pos++;
848         }
849         //Recurse if any children
850         if( b_children )
851         {
852             //Substitute p_new_item for first child leaf
853             //(If flat, continue counting from current position)
854             int i_last_pos = RecursiveAddIntoParent(
855                     p_playlist,
856                     p_new_item ? p_new_item : p_parent,
857                     p_child_node,
858                     ( b_flat ? i_pos : 0 ),
859                     b_flat,
860                     &p_new_item );
861             //If flat, take position after recursion as current position
862             if( b_flat ) i_pos = i_last_pos;
863         }
864
865         assert( p_new_item != NULL );
866         if( i == 0 ) *pp_first_leaf = p_new_item;
867     }
868     return i_pos;
869 }
870
871 static int RecursiveInsertCopy (
872     playlist_t *p_playlist, playlist_item_t *p_item,
873     playlist_item_t *p_parent, int i_pos, bool b_flat )
874 {
875     PL_ASSERT_LOCKED;
876     assert( p_parent != NULL && p_item != NULL );
877
878     if( p_item == p_parent ) return i_pos;
879
880     input_item_t *p_input = p_item->p_input;
881
882     if( !(p_item->i_children != -1 && b_flat) )
883     {
884         input_item_t *p_new_input = input_item_Copy( p_input );
885         if( !p_new_input ) return i_pos;
886
887         playlist_item_t *p_new_item = NULL;
888         if( p_item->i_children == -1 )
889             p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
890                                    p_parent, PLAYLIST_INSERT, i_pos,
891                                    pl_Locked );
892         else
893             p_new_item = playlist_NodeCreate( p_playlist, NULL,
894                                  p_parent, i_pos, 0, p_new_input );
895         vlc_gc_decref( p_new_input );
896         if( !p_new_item ) return i_pos;
897
898         i_pos++;
899
900         if( p_new_item->i_children != -1 )
901             p_parent = p_new_item;
902     }
903
904     for( int i = 0; i < p_item->i_children; i++ )
905     {
906         if( b_flat )
907             i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
908                                          p_parent, i_pos, true );
909         else
910             RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
911                                  p_parent, p_parent->i_children, false );
912     }
913
914     return i_pos;
915 }