1 /*****************************************************************************
2 * item.c : Playlist item creation/deletion/add/removal functions
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include <vlc_playlist.h>
27 #include "playlist_internal.h"
29 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
30 playlist_item_t *p_node, int i_mode, int i_pos );
31 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
32 playlist_item_t *, playlist_item_t * );
33 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
34 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
37 /*****************************************************************************
38 * An input item has gained a subitem (Event Callback)
39 *****************************************************************************/
40 static void input_item_subitem_added( const vlc_event_t * p_event,
43 playlist_t * p_playlist = user_data;
44 input_item_t * p_parent, * p_child;
45 vlc_bool_t b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
46 playlist_item_t *p_item_in_category;
47 playlist_item_t *p_current;
49 p_parent = p_event->p_obj;
50 p_child = p_event->u.input_item_subitem_added.p_new_child;
52 p_current = playlist_ItemGetByInput( p_playlist, p_parent, VLC_FALSE );
53 if( p_current->i_children == -1 )
54 p_item_in_category = playlist_ItemToNode( p_playlist, p_current,
57 p_item_in_category = p_current;
58 p_item_in_category = p_current;
59 b_play = b_play && p_current == p_playlist->status.p_item;
61 playlist_NodeAddInput( p_playlist, p_child, p_item_in_category,
62 PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
67 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
68 VLC_TRUE, p_item_in_category, NULL );
69 vlc_object_release( p_playlist );
73 /*****************************************************************************
74 * Listen to vlc_InputItemAddSubItem event
75 *****************************************************************************/
76 static void install_input_item_observer( playlist_t * p_playlist,
77 input_item_t * p_input )
79 msg_Dbg( p_playlist, "Listening to %s with %p", p_input->psz_name, p_playlist);
81 vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
82 input_item_subitem_added,
86 static void uninstall_input_item_observer( playlist_t * p_playlist,
87 input_item_t * p_input )
89 msg_Dbg( p_playlist, "Not Listening to %s with %p", p_input->psz_name, p_playlist);
91 vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
92 input_item_subitem_added,
97 /*****************************************************************************
98 * Playlist item creation
99 *****************************************************************************/
100 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
102 const char *psz_name,
104 const char *const *ppsz_options,
105 int i_duration, int i_type )
107 input_item_t *p_input;
108 if( psz_uri == NULL ) return NULL;
109 p_input = input_ItemNewWithType( p_obj, psz_uri,
110 psz_name, i_options, ppsz_options,
111 i_duration, i_type );
112 return playlist_ItemNewFromInput( p_obj, p_input );
115 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
116 input_item_t *p_input )
118 DECMALLOC_NULL( p_item, playlist_item_t );
119 playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
121 p_item->p_input = p_input;
122 vlc_gc_incref( p_item->p_input );
124 p_item->i_id = ++p_playlist->i_last_playlist_id;
126 p_item->p_parent = NULL;
127 p_item->i_children = -1;
128 p_item->pp_children = NULL;
130 p_item->p_playlist = p_playlist;
132 install_input_item_observer( p_playlist, p_input );
137 /***************************************************************************
138 * Playlist item destruction
139 ***************************************************************************/
141 /** Delete a playlist item and detach its input item */
142 int playlist_ItemDelete( playlist_item_t *p_item )
144 uninstall_input_item_observer( p_item->p_playlist, p_item->p_input );
146 vlc_gc_decref( p_item->p_input );
151 /** Remove an input item when it appears from a root playlist item */
152 static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
153 playlist_item_t *p_root, vlc_bool_t b_do_stop )
156 for( i = 0 ; i< p_root->i_children ; i++ )
158 if( p_root->pp_children[i]->i_children == -1 &&
159 p_root->pp_children[i]->p_input->i_id == i_input_id )
161 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
164 else if( p_root->pp_children[i]->i_children >= 0 )
166 int i_ret = DeleteFromInput( p_playlist, i_input_id,
167 p_root->pp_children[i], b_do_stop );
168 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
174 /** Remove an input item from ONELEVEL and CATEGORY */
175 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
176 vlc_bool_t b_locked )
179 if( !b_locked ) PL_LOCK;
180 i_ret1 = DeleteFromInput( p_playlist, i_input_id,
181 p_playlist->p_root_category, VLC_TRUE );
182 i_ret2 = DeleteFromInput( p_playlist, i_input_id,
183 p_playlist->p_root_onelevel, VLC_TRUE );
184 if( !b_locked ) PL_UNLOCK;
185 return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
186 VLC_SUCCESS : VLC_ENOITEM;
189 void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
191 if( !b_locked ) PL_LOCK;
192 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
193 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
194 if( !b_locked ) PL_UNLOCK;
197 /** Remove a playlist item from the playlist, given its id
198 * This function is to be used only by the playlist */
199 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
201 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
203 if( !p_item ) return VLC_EGENERIC;
204 return DeleteInner( p_playlist, p_item, VLC_TRUE );
207 /***************************************************************************
208 * Playlist item addition
209 ***************************************************************************/
210 /** Add an item to the playlist or the media library
211 * \param p_playlist the playlist to add into
212 * \param psz_uri the mrl to add to the playlist
213 * \param psz_name a text giving a name or description of this item
214 * \param i_mode the mode used when adding
215 * \param i_pos the position in the playlist where to add. If this is
216 * PLAYLIST_END the item will be added at the end of the playlist
217 * regardless of its size
218 * \param b_playlist TRUE for playlist, FALSE for media library
219 * \return The id of the playlist item
221 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
222 const char *psz_name, int i_mode, int i_pos,
223 vlc_bool_t b_playlist, vlc_bool_t b_locked )
225 return playlist_AddExt( p_playlist, psz_uri, psz_name,
226 i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
230 * Add a MRL into the playlist or the media library, duration and options given
232 * \param p_playlist the playlist to add into
233 * \param psz_uri the mrl to add to the playlist
234 * \param psz_name a text giving a name or description of this item
235 * \param i_mode the mode used when adding
236 * \param i_pos the position in the playlist where to add. If this is
237 * PLAYLIST_END the item will be added at the end of the playlist
238 * regardless of its size
239 * \param i_duration length of the item in milliseconds.
240 * \param ppsz_options an array of options
241 * \param i_options the number of options
242 * \param b_playlist TRUE for playlist, FALSE for media library
243 * \return The id of the playlist item
245 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
246 const char *psz_name, int i_mode, int i_pos,
247 mtime_t i_duration, const char *const *ppsz_options,
248 int i_options, vlc_bool_t b_playlist, vlc_bool_t b_locked )
251 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
252 i_options, ppsz_options,
255 i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
257 if( i_ret == VLC_SUCCESS )
258 return p_input->i_id;
262 /** Add an input item to the playlist node */
263 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
264 int i_mode, int i_pos, vlc_bool_t b_playlist,
265 vlc_bool_t b_locked )
267 playlist_item_t *p_item_cat, *p_item_one;
269 if( !p_playlist->b_doing_ml )
270 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
273 if( !b_locked ) PL_LOCK;
275 /* Add to ONELEVEL */
276 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
277 if( p_item_one == NULL ) return VLC_ENOMEM;
278 AddItem( p_playlist, p_item_one,
279 b_playlist ? p_playlist->p_local_onelevel :
280 p_playlist->p_ml_onelevel , i_mode, i_pos );
282 /* Add to CATEGORY */
283 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
284 if( p_item_cat == NULL ) return VLC_ENOMEM;
285 AddItem( p_playlist, p_item_cat,
286 b_playlist ? p_playlist->p_local_category :
287 p_playlist->p_ml_category , i_mode, i_pos );
289 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
291 if( !b_locked ) PL_UNLOCK;
295 /** Add an input item to p_direct_parent in the category tree, and to the
296 * matching top category in onelevel **/
297 int playlist_BothAddInput( playlist_t *p_playlist,
298 input_item_t *p_input,
299 playlist_item_t *p_direct_parent,
300 int i_mode, int i_pos,
301 int *i_cat, int *i_one, vlc_bool_t b_locked )
303 playlist_item_t *p_item_cat, *p_item_one, *p_up;
306 if( !b_locked ) PL_LOCK;
308 /* Add to category */
309 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
310 if( p_item_cat == NULL ) return VLC_ENOMEM;
311 AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
313 /* Add to onelevel */
314 /**Â \todo make a faster case for ml import */
315 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
316 if( p_item_one == NULL ) return VLC_ENOMEM;
318 p_up = p_direct_parent;
319 while( p_up->p_parent != p_playlist->p_root_category )
321 p_up = p_up->p_parent;
323 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
325 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
326 p_up->p_input->i_id )
328 AddItem( p_playlist, p_item_one,
329 p_playlist->p_root_onelevel->pp_children[i_top],
334 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
336 if( i_cat ) *i_cat = p_item_cat->i_id;
337 if( i_one ) *i_one = p_item_one->i_id;
339 if( !b_locked ) PL_UNLOCK;
343 /** Add an input item to a given node */
344 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
345 input_item_t *p_input,
346 playlist_item_t *p_parent,
347 int i_mode, int i_pos,
348 vlc_bool_t b_locked )
350 playlist_item_t *p_item;
352 assert( p_parent && p_parent->i_children != -1 );
354 if( !b_locked ) PL_LOCK;
356 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
357 if( p_item == NULL ) return NULL;
358 AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
360 if( !b_locked ) PL_UNLOCK;
365 /*****************************************************************************
366 * Playlist item misc operations
367 *****************************************************************************/
370 * Transform an item to a node. Return the node in the category tree, or NULL
372 * This function must be entered without the playlist lock
374 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
375 playlist_item_t *p_item,
376 vlc_bool_t b_locked )
379 playlist_item_t *p_item_in_category;
381 * Find the input in CATEGORY.
383 * - change it to node
384 * - we'll return it at the end
385 * - If we are a direct child of onelevel root, change to node, else
386 * delete the input from ONELEVEL
387 * - If we don't find it, just change to node (we are probably in VLM)
390 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
391 * useful for later BothAddInput )
394 if( !b_locked ) PL_LOCK;
396 /* Fast track the media library, no time to loose */
397 if( p_item == p_playlist->p_ml_category ) {
398 if( !b_locked ) PL_UNLOCK;
402 /** \todo First look if we don't already have it */
403 p_item_in_category = playlist_ItemFindFromInputAndRoot(
404 p_playlist, p_item->p_input->i_id,
405 p_playlist->p_root_category,
408 if( p_item_in_category )
410 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
411 p_playlist, p_item->p_input->i_id,
412 p_playlist->p_root_onelevel,
414 /* We already have it, and there is nothing more to do */
415 ChangeToNode( p_playlist, p_item_in_category );
418 return p_item_in_category;
420 /* Item in one is a root, change it to node */
421 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
422 ChangeToNode( p_playlist, p_item_in_one );
425 DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
426 p_playlist->p_root_onelevel, VLC_FALSE );
428 p_playlist->b_reset_currently_playing = VLC_TRUE;
429 vlc_cond_signal( &p_playlist->object_wait );
430 var_SetInteger( p_playlist, "item-change", p_item_in_category->
432 if( !b_locked ) PL_UNLOCK;
433 return p_item_in_category;
437 ChangeToNode( p_playlist, p_item );
438 if( !b_locked ) PL_UNLOCK;
443 /** Find an item within a root, given its input id.
444 * \return the first found item, or NULL if not found
446 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
448 playlist_item_t *p_root,
449 vlc_bool_t b_items_only )
452 for( i = 0 ; i< p_root->i_children ; i++ )
454 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
455 p_root->pp_children[i]->p_input->i_id == i_input_id )
457 return p_root->pp_children[i];
459 else if( p_root->pp_children[i]->i_children >= 0 )
461 playlist_item_t *p_search =
462 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
463 p_root->pp_children[i],
465 if( p_search ) return p_search;
472 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
473 playlist_item_t *p_node, int i_newpos )
476 playlist_item_t *p_detach = p_item->p_parent;
479 if( p_node->i_children == -1 ) return VLC_EGENERIC;
481 for( j = 0; j < p_detach->i_children; j++ )
483 if( p_detach->pp_children[j] == p_item ) break;
485 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
487 /* Attach to new parent */
488 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
489 p_item->p_parent = p_node;
497 * This function must be entered with the playlist lock
499 * \param p_playlist the playlist
500 * \param p_item the item to move
501 * \param p_node the new parent of the item
502 * \param i_newpos the new position under this new parent
503 * \return VLC_SUCCESS or an error
505 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
506 playlist_item_t *p_node, int i_newpos )
509 /* Drop on a top level node. Move in the two trees */
510 if( p_node->p_parent == p_playlist->p_root_category ||
511 p_node->p_parent == p_playlist->p_root_onelevel )
513 /* Fixme: avoid useless lookups but we need some clean helpers */
515 /* Fixme: if we try to move a node on a top-level node, it will
516 * fail because the node doesn't exist in onelevel and we will
517 * do some shit in onelevel. We should recursively move all items
519 playlist_item_t *p_node_onelevel;
520 playlist_item_t *p_item_onelevel;
521 p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
522 p_node->p_input->i_id,
523 p_playlist->p_root_onelevel,
525 p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
526 p_item->p_input->i_id,
527 p_playlist->p_root_onelevel,
529 if( p_node_onelevel && p_item_onelevel )
530 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
533 playlist_item_t *p_node_category;
534 playlist_item_t *p_item_category;
535 p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
536 p_node->p_input->i_id,
537 p_playlist->p_root_category,
539 p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
540 p_item->p_input->i_id,
541 p_playlist->p_root_category,
543 if( p_node_category && p_item_category )
544 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
549 i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
550 p_playlist->b_reset_currently_playing = VLC_TRUE;
551 vlc_cond_signal( &p_playlist->object_wait );
555 /** Send a notification that an item has been added to a node */
556 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
557 int i_node_id, vlc_bool_t b_signal )
560 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
561 p_add->i_item = i_item_id;
562 p_add->i_node = i_node_id;
563 val.p_address = p_add;
564 p_playlist->b_reset_currently_playing = VLC_TRUE;
566 vlc_cond_signal( &p_playlist->object_wait );
567 var_Set( p_playlist, "item-append", val );
571 /*****************************************************************************
572 * Playlist item accessors
573 *****************************************************************************/
575 /** Set the name of a playlist item */
576 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
578 if( psz_name && p_item )
580 input_ItemSetName( p_item->p_input, psz_name );
586 /***************************************************************************
587 * The following functions are local
588 ***************************************************************************/
590 /* Enqueue an item for preparsing, and play it, if needed */
591 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
592 playlist_item_t *p_item_cat,
593 playlist_item_t *p_item_one )
595 if( (i_mode & PLAYLIST_GO ) )
597 playlist_item_t *p_parent = p_item_one;
598 playlist_item_t *p_toplay = NULL;
601 if( p_parent == p_playlist->p_root_category )
603 p_toplay = p_item_cat; break;
605 else if( p_parent == p_playlist->p_root_onelevel )
607 p_toplay = p_item_one; break;
609 p_parent = p_parent->p_parent;
612 p_playlist->request.b_request = VLC_TRUE;
613 p_playlist->request.i_skip = 0;
614 p_playlist->request.p_item = p_toplay;
615 if( p_playlist->p_input )
616 input_StopThread( p_playlist->p_input );
617 p_playlist->request.i_status = PLAYLIST_RUNNING;
618 vlc_cond_signal( &p_playlist->object_wait );
620 /* Preparse if PREPARSE or SPREPARSE & not enough meta */
621 if( p_playlist->b_auto_preparse &&
622 (i_mode & PLAYLIST_PREPARSE ||
623 ( i_mode & PLAYLIST_SPREPARSE &&
624 ( EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) ||
625 ( EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) ) )
627 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
628 /* If we already have it, signal it */
629 else if( !EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) &&
630 !EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) )
631 input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
634 /* Add the playlist item to the requested node and fire a notification */
635 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
636 playlist_item_t *p_node, int i_mode, int i_pos )
638 ARRAY_APPEND(p_playlist->items, p_item);
639 ARRAY_APPEND(p_playlist->all_items, p_item);
640 p_playlist->i_enabled ++;
642 if( i_pos == PLAYLIST_END )
643 playlist_NodeAppend( p_playlist, p_item, p_node );
645 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
647 if( !p_playlist->b_doing_ml )
648 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
649 !( i_mode & PLAYLIST_NO_REBUILD ) );
652 /* Actually convert an item to a node */
653 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
656 if( p_item->i_children == -1 )
657 p_item->i_children = 0;
659 /* Remove it from the array of available items */
660 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
662 ARRAY_REMOVE( p_playlist->items, i );
665 /* Do the actual removal */
666 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
670 int i_id = p_item->i_id;
671 vlc_bool_t b_delay_deletion = VLC_FALSE;
673 if( p_item->i_children > -1 )
675 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
677 p_playlist->b_reset_currently_playing = VLC_TRUE;
678 var_SetInteger( p_playlist, "item-deleted", i_id );
680 /* Remove the item from the bank */
681 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
683 ARRAY_REMOVE( p_playlist->all_items, i );
685 /* Check if it is the current item */
686 if( p_playlist->status.p_item == p_item )
688 /* Hack we don't call playlist_Control for lock reasons */
691 p_playlist->request.i_status = PLAYLIST_STOPPED;
692 p_playlist->request.b_request = VLC_TRUE;
693 p_playlist->request.p_item = NULL;
694 msg_Info( p_playlist, "stopping playback" );
695 vlc_cond_signal( &p_playlist->object_wait );
697 b_delay_deletion = VLC_TRUE;
700 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
702 /* Remove the item from its parent */
703 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
705 if( !b_delay_deletion )
706 playlist_ItemDelete( p_item );
709 PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
710 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;