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 *****************************************************************************/
25 #include <vlc/input.h>
27 #include <vlc_playlist.h>
28 #include "playlist_internal.h"
30 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
31 playlist_item_t *p_node, int i_pos );
32 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
33 playlist_item_t *, playlist_item_t * );
34 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
35 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
38 /*****************************************************************************
39 * Playlist item creation
40 *****************************************************************************/
41 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
45 const char *const *ppsz_options,
46 int i_duration, int i_type )
48 input_item_t *p_input;
49 if( psz_uri == NULL ) return NULL;
50 p_input = input_ItemNewWithType( p_obj, psz_uri,
51 psz_name, i_options, ppsz_options,
53 return playlist_ItemNewFromInput( p_obj, p_input );
56 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
57 input_item_t *p_input )
59 DECMALLOC_NULL( p_item, playlist_item_t );
60 playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
61 vlc_object_yield( p_playlist );
63 p_item->p_input = p_input;
64 vlc_gc_incref( p_item->p_input );
66 p_item->i_id = ++p_playlist->i_last_playlist_id;
68 p_item->p_parent = NULL;
69 p_item->i_children = -1;
70 p_item->pp_children = NULL;
73 vlc_object_release( p_playlist );
78 /***************************************************************************
79 * Playlist item destruction
80 ***************************************************************************/
82 /** Delete a playlist item and detach its input item */
83 int playlist_ItemDelete( playlist_item_t *p_item )
85 vlc_gc_decref( p_item->p_input );
90 /** Remove an input item from ONELEVEL and CATEGORY */
91 int playlist_DeleteAllFromInput( playlist_t *p_playlist, int i_input_id )
93 playlist_DeleteFromInput( p_playlist, i_input_id,
94 p_playlist->p_root_category, VLC_TRUE );
95 playlist_DeleteFromInput( p_playlist, i_input_id,
96 p_playlist->p_root_onelevel, VLC_TRUE );
100 /** Remove an input item from ONELEVEL and CATEGORY.
101 * This function must be entered without the playlist lock */
102 int playlist_LockDeleteAllFromInput( playlist_t * p_playlist, int i_id )
105 vlc_mutex_lock( &p_playlist->object_lock );
106 i_ret = playlist_DeleteAllFromInput( p_playlist, i_id );
107 vlc_mutex_unlock( &p_playlist->object_lock );
111 /** Remove an input item when it appears from a root playlist item */
112 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
113 playlist_item_t *p_root, vlc_bool_t b_do_stop )
116 for( i = 0 ; i< p_root->i_children ; i++ )
118 if( p_root->pp_children[i]->i_children == -1 &&
119 p_root->pp_children[i]->p_input->i_id == i_input_id )
121 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
124 else if( p_root->pp_children[i]->i_children >= 0 )
126 int i_ret = playlist_DeleteFromInput( p_playlist, i_input_id,
127 p_root->pp_children[i], b_do_stop );
128 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
134 /** Remove a playlist item from the playlist, given its id */
135 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
137 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
138 if( !p_item ) return VLC_EGENERIC;
139 return DeleteInner( p_playlist, p_item, VLC_TRUE );
142 /** Remove a playlist item from the playlist, given its id
143 * This function should be entered without the playlist lock */
144 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
147 vlc_mutex_lock( &p_playlist->object_lock );
148 i_ret = playlist_DeleteFromItemId( p_playlist, i_id );
149 vlc_mutex_unlock( &p_playlist->object_lock );
153 /** Clear the playlist */
154 void playlist_Clear( playlist_t * p_playlist )
156 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
157 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
159 /** Clear the playlist. This function must be entered without the lock */
160 void playlist_LockClear( playlist_t *p_playlist )
162 vlc_mutex_lock( &p_playlist->object_lock );
163 playlist_Clear( p_playlist );
164 vlc_mutex_unlock( &p_playlist->object_lock );
167 /***************************************************************************
168 * Playlist item addition
169 ***************************************************************************/
170 /** Add an item to the playlist or the media library
171 * \param p_playlist the playlist to add into
172 * \param psz_uri the mrl to add to the playlist
173 * \param psz_name a text giving a name or description of this item
174 * \param i_mode the mode used when adding
175 * \param i_pos the position in the playlist where to add. If this is
176 * PLAYLIST_END the item will be added at the end of the playlist
177 * regardless of its size
178 * \param b_playlist TRUE for playlist, FALSE for media library
179 * \return The id of the playlist item
181 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
182 const char *psz_name, int i_mode, int i_pos,
183 vlc_bool_t b_playlist )
185 return playlist_AddExt( p_playlist, psz_uri, psz_name,
186 i_mode, i_pos, -1, NULL, 0, b_playlist );
190 * Add a MRL into the playlist or the media library, duration and options given
192 * \param p_playlist the playlist to add into
193 * \param psz_uri the mrl to add to the playlist
194 * \param psz_name a text giving a name or description of this item
195 * \param i_mode the mode used when adding
196 * \param i_pos the position in the playlist where to add. If this is
197 * PLAYLIST_END the item will be added at the end of the playlist
198 * regardless of its size
199 * \param i_duration length of the item in milliseconds.
200 * \param ppsz_options an array of options
201 * \param i_options the number of options
202 * \param b_playlist TRUE for playlist, FALSE for media library
203 * \return The id of the playlist item
205 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
206 const char *psz_name, int i_mode, int i_pos,
207 mtime_t i_duration, const char *const *ppsz_options,
208 int i_options, vlc_bool_t b_playlist )
210 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
211 i_options, ppsz_options,
214 return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist );
217 /** Add an input item to the playlist node */
218 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
219 int i_mode, int i_pos, vlc_bool_t b_playlist )
221 playlist_item_t *p_item_cat, *p_item_one;
223 if( !p_playlist->b_doing_ml )
224 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
227 vlc_mutex_lock( &p_playlist->object_lock );
229 /* Add to ONELEVEL */
230 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
231 if( p_item_one == NULL ) return VLC_EGENERIC;
232 AddItem( p_playlist, p_item_one,
233 b_playlist ? p_playlist->p_local_onelevel :
234 p_playlist->p_ml_onelevel , i_pos );
236 /* Add to CATEGORY */
237 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
238 if( p_item_cat == NULL ) return VLC_EGENERIC;
239 AddItem( p_playlist, p_item_cat,
240 b_playlist ? p_playlist->p_local_category :
241 p_playlist->p_ml_category , i_pos );
243 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
245 vlc_mutex_unlock( &p_playlist->object_lock );
249 /** Add an input item to p_direct_parent in the category tree, and to the
250 * matching top category in onelevel **/
251 int playlist_BothAddInput( playlist_t *p_playlist,
252 input_item_t *p_input,
253 playlist_item_t *p_direct_parent,
254 int i_mode, int i_pos )
256 playlist_item_t *p_item_cat, *p_item_one, *p_up;
259 vlc_mutex_lock( & p_playlist->object_lock );
261 /* Add to category */
262 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
263 if( p_item_cat == NULL ) return VLC_EGENERIC;
264 AddItem( p_playlist, p_item_cat, p_direct_parent, i_pos );
266 /* Add to onelevel */
267 /**Â \todo make a faster case for ml import */
268 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
269 if( p_item_one == NULL ) return VLC_EGENERIC;
271 p_up = p_direct_parent;
272 while( p_up->p_parent != p_playlist->p_root_category )
274 p_up = p_up->p_parent;
276 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
278 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
279 p_up->p_input->i_id )
281 AddItem( p_playlist, p_item_one,
282 p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
286 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
288 vlc_mutex_unlock( &p_playlist->object_lock );
292 /** Add an input item to a given node */
293 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
294 input_item_t *p_input,
295 playlist_item_t *p_parent,
296 int i_mode, int i_pos )
298 playlist_item_t *p_item;
300 assert( p_parent && p_parent->i_children != -1 );
302 vlc_mutex_lock( &p_playlist->object_lock );
304 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
305 if( p_item == NULL ) return NULL;
306 AddItem( p_playlist, p_item, p_parent, i_pos );
308 vlc_mutex_unlock( &p_playlist->object_lock );
313 /** Add a playlist item to a given node */
314 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
315 playlist_item_t *p_parent, int i_mode, int i_pos )
317 vlc_mutex_lock( &p_playlist->object_lock );
318 AddItem( p_playlist, p_item, p_parent, i_pos );
319 vlc_mutex_unlock( &p_playlist->object_lock );
322 /*****************************************************************************
323 * Playlist item misc operations
324 *****************************************************************************/
327 * Transform an item to a node. Return the node in the category tree, or NULL
329 * This function must be entered without the playlist lock
331 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
332 playlist_item_t *p_item )
335 playlist_item_t *p_item_in_category;
337 * Find the input in CATEGORY.
339 * - change it to node
340 * - we'll return it at the end
341 * - If we are a direct child of onelevel root, change to node, else
342 * delete the input from ONELEVEL
343 * - If we don't find it, just change to node (we are probably in VLM)
346 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
347 * useful for later BothAddInput )
350 /* Fast track the media library, no time to loose */
351 if( p_item == p_playlist->p_ml_category )
354 /** \todo First look if we don't already have it */
355 p_item_in_category = playlist_ItemFindFromInputAndRoot(
356 p_playlist, p_item->p_input->i_id,
357 p_playlist->p_root_category,
360 if( p_item_in_category )
362 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
363 p_playlist, p_item->p_input->i_id,
364 p_playlist->p_root_onelevel,
366 assert( p_item_in_one );
367 ChangeToNode( p_playlist, p_item_in_category );
368 /* Item in one is a root, change it to node */
369 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
370 ChangeToNode( p_playlist, p_item_in_one );
373 playlist_DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
374 p_playlist->p_root_onelevel, VLC_FALSE );
376 p_playlist->b_reset_currently_playing = VLC_TRUE;
377 vlc_cond_signal( &p_playlist->object_wait );
378 var_SetInteger( p_playlist, "item-change", p_item_in_category->
380 return p_item_in_category;
384 ChangeToNode( p_playlist, p_item );
389 /** Transform an item to a node
390 * This function must be entered without the playlist lock
391 * \see playlist_ItemToNode
393 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
394 playlist_item_t *p_item )
396 playlist_item_t *p_ret;
397 vlc_mutex_lock( &p_playlist->object_lock );
398 p_ret = playlist_ItemToNode( p_playlist, p_item );
399 vlc_mutex_unlock( &p_playlist->object_lock );
403 /** Find an item within a root, given its input id.
404 * \return the first found item, or NULL if not found
406 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
408 playlist_item_t *p_root,
409 vlc_bool_t b_items_only )
412 for( i = 0 ; i< p_root->i_children ; i++ )
414 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
415 p_root->pp_children[i]->p_input->i_id == i_input_id )
417 return p_root->pp_children[i];
419 else if( p_root->pp_children[i]->i_children >= 0 )
421 playlist_item_t *p_search =
422 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
423 p_root->pp_children[i],
425 if( p_search ) return p_search;
432 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
433 playlist_item_t *p_node, int i_newpos )
436 playlist_item_t *p_detach = p_item->p_parent;
437 if( p_node->i_children == -1 ) return VLC_EGENERIC;
439 for( j = 0; j < p_detach->i_children; j++ )
441 if( p_detach->pp_children[j] == p_item ) break;
443 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
445 /* Attach to new parent */
446 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
447 p_item->p_parent = p_node;
455 * This function must be entered with the playlist lock
457 * \param p_playlist the playlist
458 * \param p_item the item to move
459 * \param p_node the new parent of the item
460 * \param i_newpos the new position under this new parent
461 * \return VLC_SUCCESS or an error
463 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
464 playlist_item_t *p_node, int i_newpos )
467 /* Drop on a top level node. Move in the two trees */
468 if( p_node->p_parent == p_playlist->p_root_category ||
469 p_node->p_parent == p_playlist->p_root_onelevel )
471 /* Fixme: avoid useless lookups but we need some clean helpers */
473 /* Fixme: if we try to move a node on a top-level node, it will
474 * fail because the node doesn't exist in onelevel and we will
475 * do some shit in onelevel. We should recursively move all items
477 playlist_item_t *p_node_onelevel;
478 playlist_item_t *p_item_onelevel;
479 p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
480 p_node->p_input->i_id,
481 p_playlist->p_root_onelevel,
483 p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
484 p_item->p_input->i_id,
485 p_playlist->p_root_onelevel,
487 if( p_node_onelevel && p_item_onelevel )
488 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
491 playlist_item_t *p_node_category;
492 playlist_item_t *p_item_category;
493 p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
494 p_node->p_input->i_id,
495 p_playlist->p_root_category,
497 p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
498 p_item->p_input->i_id,
499 p_playlist->p_root_category,
501 if( p_node_category && p_item_category )
502 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
507 i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
508 p_playlist->b_reset_currently_playing = VLC_TRUE;
509 vlc_cond_signal( &p_playlist->object_wait );
513 /** Send a notification that an item has been added to a node */
514 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
518 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
519 p_add->i_item = i_item_id;
520 p_add->i_node = i_node_id;
521 val.p_address = p_add;
522 p_playlist->b_reset_currently_playing = VLC_TRUE;
523 vlc_cond_signal( &p_playlist->object_wait );
524 var_Set( p_playlist, "item-append", val );
528 /*****************************************************************************
529 * Playlist item accessors
530 *****************************************************************************/
532 /** Set the name of a playlist item */
533 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
535 if( psz_name && p_item )
537 if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
538 p_item->p_input->psz_name = strdup( psz_name );
544 /***************************************************************************
545 * The following functions are local
546 ***************************************************************************/
548 /* Enqueue an item for preparsing, and play it, if needed */
549 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
550 playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
552 if( (i_mode & PLAYLIST_GO ) )
554 playlist_item_t *p_parent = p_item_one;
555 playlist_item_t *p_toplay = NULL;
558 if( p_parent == p_playlist->p_root_category )
560 p_toplay = p_item_cat; break;
562 else if( p_parent == p_playlist->p_root_onelevel )
564 p_toplay = p_item_one; break;
566 p_parent = p_parent->p_parent;
569 p_playlist->request.b_request = VLC_TRUE;
570 p_playlist->request.i_skip = 0;
571 p_playlist->request.p_item = p_toplay;
572 if( p_playlist->p_input )
573 input_StopThread( p_playlist->p_input );
574 p_playlist->request.i_status = PLAYLIST_RUNNING;
575 vlc_cond_signal( &p_playlist->object_wait );
577 /* Preparse if PREPARSE or SPREPARSE & not enough meta */
578 if( p_playlist->b_auto_preparse &&
579 (i_mode & PLAYLIST_PREPARSE ||
580 ( i_mode & PLAYLIST_SPREPARSE &&
581 ( !p_item_cat->p_input->p_meta || (p_item_cat->p_input->p_meta &&
582 ( EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) ||
583 EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
587 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
588 /* If we already have it, signal it */
589 else if( p_item_cat->p_input->p_meta &&
590 !EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) &&
591 !EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
592 p_item_cat->p_input->p_meta->i_status = ITEM_PREPARSED;
595 /* Add the playlist item to the requested node and fire a notification */
596 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
597 playlist_item_t *p_node, int i_pos )
599 ARRAY_APPEND(p_playlist->items, p_item);
600 ARRAY_APPEND(p_playlist->all_items, p_item);
601 p_playlist->i_enabled ++;
603 if( i_pos == PLAYLIST_END )
604 playlist_NodeAppend( p_playlist, p_item, p_node );
606 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
608 if( !p_playlist->b_doing_ml )
609 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
612 /* Actually convert an item to a node */
613 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
616 if( p_item->i_children == -1 )
617 p_item->i_children = 0;
619 /* Remove it from the array of available items */
620 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
622 ARRAY_REMOVE( p_playlist->items, i );
625 /* Do the actual removal */
626 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
630 int i_id = p_item->i_id;
631 vlc_bool_t b_flag = VLC_FALSE;
633 if( p_item->i_children > -1 )
635 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
637 p_playlist->b_reset_currently_playing = VLC_TRUE;
638 var_SetInteger( p_playlist, "item-deleted", i_id );
640 /* Remove the item from the bank */
641 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
643 ARRAY_REMOVE( p_playlist->all_items, i );
645 /* Check if it is the current item */
646 if( p_playlist->status.p_item == p_item )
648 /* Hack we don't call playlist_Control for lock reasons */
651 p_playlist->request.i_status = PLAYLIST_STOPPED;
652 p_playlist->request.b_request = VLC_TRUE;
653 p_playlist->request.p_item = NULL;
654 msg_Info( p_playlist, "stopping playback" );
655 vlc_cond_signal( &p_playlist->object_wait );
660 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
662 /* Remove the item from its parent */
663 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
665 if( b_flag == VLC_FALSE )
666 playlist_ItemDelete( p_item );
669 PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
670 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;