1 /*****************************************************************************
2 * item.c : Playlist item creation/deletion/add/removal functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 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 * Playlist item creation
39 *****************************************************************************/
40 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
44 const char *const *ppsz_options,
45 int i_duration, int i_type )
47 input_item_t *p_input;
48 if( psz_uri == NULL ) return NULL;
49 p_input = input_ItemNewWithType( p_obj, psz_uri,
50 psz_name, i_options, ppsz_options,
52 return playlist_ItemNewFromInput( p_obj, p_input );
55 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
56 input_item_t *p_input )
58 DECMALLOC_NULL( p_item, playlist_item_t );
59 playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
60 vlc_object_yield( p_playlist );
62 p_item->p_input = p_input;
63 vlc_gc_incref( p_item->p_input );
65 p_item->i_id = ++p_playlist->i_last_playlist_id;
67 p_item->p_parent = NULL;
68 p_item->i_children = -1;
69 p_item->pp_children = NULL;
72 vlc_object_release( p_playlist );
77 /***************************************************************************
78 * Playlist item destruction
79 ***************************************************************************/
81 /** Delete a playlist item and detach its input item */
82 int playlist_ItemDelete( playlist_item_t *p_item )
84 vlc_gc_decref( p_item->p_input );
89 /** Remove an input item when it appears from a root playlist item */
90 static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
91 playlist_item_t *p_root, vlc_bool_t b_do_stop )
94 for( i = 0 ; i< p_root->i_children ; i++ )
96 if( p_root->pp_children[i]->i_children == -1 &&
97 p_root->pp_children[i]->p_input->i_id == i_input_id )
99 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
102 else if( p_root->pp_children[i]->i_children >= 0 )
104 int i_ret = DeleteFromInput( p_playlist, i_input_id,
105 p_root->pp_children[i], b_do_stop );
106 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
112 /** Remove an input item from ONELEVEL and CATEGORY */
113 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
114 vlc_bool_t b_locked )
117 if( !b_locked ) PL_LOCK;
118 i_ret1 = DeleteFromInput( p_playlist, i_input_id,
119 p_playlist->p_root_category, VLC_TRUE );
120 i_ret2 = DeleteFromInput( p_playlist, i_input_id,
121 p_playlist->p_root_onelevel, VLC_TRUE );
122 if( !b_locked ) PL_UNLOCK;
123 return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
124 VLC_SUCCESS : VLC_ENOITEM;
127 void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
129 if( !b_locked ) PL_LOCK;
130 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
131 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
132 if( !b_locked ) PL_UNLOCK;
135 /** Remove a playlist item from the playlist, given its id
136 * This function is to be used only by the playlist */
137 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
139 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id, VLC_TRUE );
140 if( !p_item ) return VLC_EGENERIC;
141 return DeleteInner( p_playlist, p_item, VLC_TRUE );
144 /***************************************************************************
145 * Playlist item addition
146 ***************************************************************************/
147 /** Add an item to the playlist or the media library
148 * \param p_playlist the playlist to add into
149 * \param psz_uri the mrl to add to the playlist
150 * \param psz_name a text giving a name or description of this item
151 * \param i_mode the mode used when adding
152 * \param i_pos the position in the playlist where to add. If this is
153 * PLAYLIST_END the item will be added at the end of the playlist
154 * regardless of its size
155 * \param b_playlist TRUE for playlist, FALSE for media library
156 * \return The id of the playlist item
158 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
159 const char *psz_name, int i_mode, int i_pos,
160 vlc_bool_t b_playlist )
162 return playlist_AddExt( p_playlist, psz_uri, psz_name,
163 i_mode, i_pos, -1, NULL, 0, b_playlist );
167 * Add a MRL into the playlist or the media library, duration and options given
169 * \param p_playlist the playlist to add into
170 * \param psz_uri the mrl to add to the playlist
171 * \param psz_name a text giving a name or description of this item
172 * \param i_mode the mode used when adding
173 * \param i_pos the position in the playlist where to add. If this is
174 * PLAYLIST_END the item will be added at the end of the playlist
175 * regardless of its size
176 * \param i_duration length of the item in milliseconds.
177 * \param ppsz_options an array of options
178 * \param i_options the number of options
179 * \param b_playlist TRUE for playlist, FALSE for media library
180 * \return The id of the playlist item
182 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
183 const char *psz_name, int i_mode, int i_pos,
184 mtime_t i_duration, const char *const *ppsz_options,
185 int i_options, vlc_bool_t b_playlist )
188 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
189 i_options, ppsz_options,
192 i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist );
193 if( i_ret == VLC_SUCCESS )
194 return p_input->i_id;
198 /** Add an input item to the playlist node */
199 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
200 int i_mode, int i_pos, vlc_bool_t b_playlist )
202 playlist_item_t *p_item_cat, *p_item_one;
204 if( !p_playlist->b_doing_ml )
205 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
210 /* Add to ONELEVEL */
211 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
212 if( p_item_one == NULL ) return VLC_ENOMEM;
213 AddItem( p_playlist, p_item_one,
214 b_playlist ? p_playlist->p_local_onelevel :
215 p_playlist->p_ml_onelevel , i_mode, i_pos );
217 /* Add to CATEGORY */
218 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
219 if( p_item_cat == NULL ) return VLC_ENOMEM;
220 AddItem( p_playlist, p_item_cat,
221 b_playlist ? p_playlist->p_local_category :
222 p_playlist->p_ml_category , i_mode, i_pos );
224 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
230 /** Add an input item to p_direct_parent in the category tree, and to the
231 * matching top category in onelevel **/
232 int playlist_BothAddInput( playlist_t *p_playlist,
233 input_item_t *p_input,
234 playlist_item_t *p_direct_parent,
235 int i_mode, int i_pos,
236 int *i_cat, int *i_one )
238 playlist_item_t *p_item_cat, *p_item_one, *p_up;
241 vlc_mutex_lock( & p_playlist->object_lock );
243 /* Add to category */
244 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
245 if( p_item_cat == NULL ) return VLC_ENOMEM;
246 AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
248 /* Add to onelevel */
249 /** \todo make a faster case for ml import */
250 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
251 if( p_item_one == NULL ) return VLC_ENOMEM;
253 p_up = p_direct_parent;
254 while( p_up->p_parent != p_playlist->p_root_category )
256 p_up = p_up->p_parent;
258 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
260 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
261 p_up->p_input->i_id )
263 AddItem( p_playlist, p_item_one,
264 p_playlist->p_root_onelevel->pp_children[i_top],
269 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
271 if( i_cat ) *i_cat = p_item_cat->i_id;
272 if( i_one ) *i_one = p_item_one->i_id;
274 vlc_mutex_unlock( &p_playlist->object_lock );
278 /** Add an input item to a given node */
279 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
280 input_item_t *p_input,
281 playlist_item_t *p_parent,
282 int i_mode, int i_pos )
284 playlist_item_t *p_item;
286 assert( p_parent && p_parent->i_children != -1 );
288 vlc_mutex_lock( &p_playlist->object_lock );
290 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
291 if( p_item == NULL ) return NULL;
292 AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
294 vlc_mutex_unlock( &p_playlist->object_lock );
299 /*****************************************************************************
300 * Playlist item misc operations
301 *****************************************************************************/
304 * Transform an item to a node. Return the node in the category tree, or NULL
306 * This function must be entered without the playlist lock
308 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
309 playlist_item_t *p_item,
310 vlc_bool_t b_locked )
313 playlist_item_t *p_item_in_category;
315 * Find the input in CATEGORY.
317 * - change it to node
318 * - we'll return it at the end
319 * - If we are a direct child of onelevel root, change to node, else
320 * delete the input from ONELEVEL
321 * - If we don't find it, just change to node (we are probably in VLM)
324 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
325 * useful for later BothAddInput )
328 if( !b_locked ) PL_LOCK;
330 /* Fast track the media library, no time to loose */
331 if( p_item == p_playlist->p_ml_category ) {
332 if( !b_locked ) PL_UNLOCK;
336 /** \todo First look if we don't already have it */
337 p_item_in_category = playlist_ItemFindFromInputAndRoot(
338 p_playlist, p_item->p_input->i_id,
339 p_playlist->p_root_category,
342 if( p_item_in_category )
344 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
345 p_playlist, p_item->p_input->i_id,
346 p_playlist->p_root_onelevel,
348 assert( p_item_in_one );
349 ChangeToNode( p_playlist, p_item_in_category );
350 /* Item in one is a root, change it to node */
351 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
352 ChangeToNode( p_playlist, p_item_in_one );
355 DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
356 p_playlist->p_root_onelevel, VLC_FALSE );
358 p_playlist->b_reset_currently_playing = VLC_TRUE;
359 vlc_cond_signal( &p_playlist->object_wait );
360 var_SetInteger( p_playlist, "item-change", p_item_in_category->
362 if( !b_locked ) PL_UNLOCK;
363 return p_item_in_category;
367 ChangeToNode( p_playlist, p_item );
368 if( !b_locked ) PL_UNLOCK;
373 /** Find an item within a root, given its input id.
374 * \return the first found item, or NULL if not found
376 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
378 playlist_item_t *p_root,
379 vlc_bool_t b_items_only )
382 for( i = 0 ; i< p_root->i_children ; i++ )
384 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
385 p_root->pp_children[i]->p_input->i_id == i_input_id )
387 return p_root->pp_children[i];
389 else if( p_root->pp_children[i]->i_children >= 0 )
391 playlist_item_t *p_search =
392 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
393 p_root->pp_children[i],
395 if( p_search ) return p_search;
402 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
403 playlist_item_t *p_node, int i_newpos )
406 playlist_item_t *p_detach = p_item->p_parent;
407 if( p_node->i_children == -1 ) return VLC_EGENERIC;
409 for( j = 0; j < p_detach->i_children; j++ )
411 if( p_detach->pp_children[j] == p_item ) break;
413 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
415 /* Attach to new parent */
416 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
417 p_item->p_parent = p_node;
425 * This function must be entered with the playlist lock
427 * \param p_playlist the playlist
428 * \param p_item the item to move
429 * \param p_node the new parent of the item
430 * \param i_newpos the new position under this new parent
431 * \return VLC_SUCCESS or an error
433 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
434 playlist_item_t *p_node, int i_newpos )
437 /* Drop on a top level node. Move in the two trees */
438 if( p_node->p_parent == p_playlist->p_root_category ||
439 p_node->p_parent == p_playlist->p_root_onelevel )
441 /* Fixme: avoid useless lookups but we need some clean helpers */
443 /* Fixme: if we try to move a node on a top-level node, it will
444 * fail because the node doesn't exist in onelevel and we will
445 * do some shit in onelevel. We should recursively move all items
447 playlist_item_t *p_node_onelevel;
448 playlist_item_t *p_item_onelevel;
449 p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
450 p_node->p_input->i_id,
451 p_playlist->p_root_onelevel,
453 p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
454 p_item->p_input->i_id,
455 p_playlist->p_root_onelevel,
457 if( p_node_onelevel && p_item_onelevel )
458 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
461 playlist_item_t *p_node_category;
462 playlist_item_t *p_item_category;
463 p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
464 p_node->p_input->i_id,
465 p_playlist->p_root_category,
467 p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
468 p_item->p_input->i_id,
469 p_playlist->p_root_category,
471 if( p_node_category && p_item_category )
472 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
477 i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
478 p_playlist->b_reset_currently_playing = VLC_TRUE;
479 vlc_cond_signal( &p_playlist->object_wait );
483 /** Send a notification that an item has been added to a node */
484 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
485 int i_node_id, vlc_bool_t b_signal )
488 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
489 p_add->i_item = i_item_id;
490 p_add->i_node = i_node_id;
491 val.p_address = p_add;
492 p_playlist->b_reset_currently_playing = VLC_TRUE;
494 vlc_cond_signal( &p_playlist->object_wait );
495 var_Set( p_playlist, "item-append", val );
499 /*****************************************************************************
500 * Playlist item accessors
501 *****************************************************************************/
503 /** Set the name of a playlist item */
504 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
506 if( psz_name && p_item )
508 if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
509 p_item->p_input->psz_name = strdup( psz_name );
515 /***************************************************************************
516 * The following functions are local
517 ***************************************************************************/
519 /* Enqueue an item for preparsing, and play it, if needed */
520 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
521 playlist_item_t *p_item_cat,
522 playlist_item_t *p_item_one )
524 if( (i_mode & PLAYLIST_GO ) )
526 playlist_item_t *p_parent = p_item_one;
527 playlist_item_t *p_toplay = NULL;
530 if( p_parent == p_playlist->p_root_category )
532 p_toplay = p_item_cat; break;
534 else if( p_parent == p_playlist->p_root_onelevel )
536 p_toplay = p_item_one; break;
538 p_parent = p_parent->p_parent;
541 p_playlist->request.b_request = VLC_TRUE;
542 p_playlist->request.i_skip = 0;
543 p_playlist->request.p_item = p_toplay;
544 if( p_playlist->p_input )
545 input_StopThread( p_playlist->p_input );
546 p_playlist->request.i_status = PLAYLIST_RUNNING;
547 vlc_cond_signal( &p_playlist->object_wait );
549 /* Preparse if PREPARSE or SPREPARSE & not enough meta */
550 if( p_playlist->b_auto_preparse &&
551 (i_mode & PLAYLIST_PREPARSE ||
552 ( i_mode & PLAYLIST_SPREPARSE &&
553 ( !p_item_cat->p_input->p_meta || (p_item_cat->p_input->p_meta &&
554 ( EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) ||
555 EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
559 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
560 /* If we already have it, signal it */
561 else if( p_item_cat->p_input->p_meta &&
562 !EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) &&
563 !EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
564 p_item_cat->p_input->p_meta->i_status = ITEM_PREPARSED;
567 /* Add the playlist item to the requested node and fire a notification */
568 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
569 playlist_item_t *p_node, int i_mode, int i_pos )
571 ARRAY_APPEND(p_playlist->items, p_item);
572 ARRAY_APPEND(p_playlist->all_items, p_item);
573 p_playlist->i_enabled ++;
575 if( i_pos == PLAYLIST_END )
576 playlist_NodeAppend( p_playlist, p_item, p_node );
578 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
580 if( !p_playlist->b_doing_ml )
581 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
582 !( i_mode & PLAYLIST_NO_REBUILD ) );
585 /* Actually convert an item to a node */
586 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
589 if( p_item->i_children == -1 )
590 p_item->i_children = 0;
592 /* Remove it from the array of available items */
593 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
595 ARRAY_REMOVE( p_playlist->items, i );
598 /* Do the actual removal */
599 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
603 int i_id = p_item->i_id;
604 vlc_bool_t b_flag = VLC_FALSE;
606 if( p_item->i_children > -1 )
608 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
610 p_playlist->b_reset_currently_playing = VLC_TRUE;
611 var_SetInteger( p_playlist, "item-deleted", i_id );
613 /* Remove the item from the bank */
614 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
616 ARRAY_REMOVE( p_playlist->all_items, i );
618 /* Check if it is the current item */
619 if( p_playlist->status.p_item == p_item )
621 /* Hack we don't call playlist_Control for lock reasons */
624 p_playlist->request.i_status = PLAYLIST_STOPPED;
625 p_playlist->request.b_request = VLC_TRUE;
626 p_playlist->request.p_item = NULL;
627 msg_Info( p_playlist, "stopping playback" );
628 vlc_cond_signal( &p_playlist->object_wait );
633 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
635 /* Remove the item from its parent */
636 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
638 if( b_flag == VLC_FALSE )
639 playlist_ItemDelete( p_item );
642 PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
643 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;