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"
29 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
30 playlist_item_t *p_node, int i_pos );
31 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
32 playlist_item_t *, playlist_item_t * );
33 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
34 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 **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 /** FIXME !!!!! don't find playlist each time */
59 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_obj, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
60 DECMALLOC_NULL( p_item, playlist_item_t );
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 from ONELEVEL and CATEGORY */
90 int playlist_DeleteAllFromInput( playlist_t *p_playlist, int i_input_id )
92 playlist_DeleteFromInput( p_playlist, i_input_id,
93 p_playlist->p_root_category, VLC_TRUE );
94 playlist_DeleteFromInput( p_playlist, i_input_id,
95 p_playlist->p_root_onelevel, VLC_TRUE );
99 /** Remove an input item from ONELEVEL and CATEGORY.
100 * This function must be entered without the playlist lock */
101 int playlist_LockDeleteAllFromInput( playlist_t * p_playlist, int i_id )
104 vlc_mutex_lock( &p_playlist->object_lock );
105 i_ret = playlist_DeleteAllFromInput( p_playlist, i_id );
106 vlc_mutex_unlock( &p_playlist->object_lock );
110 /** Remove an input item when it appears from a root playlist item */
111 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
112 playlist_item_t *p_root, vlc_bool_t b_do_stop )
115 for( i = 0 ; i< p_root->i_children ; i++ )
117 if( p_root->pp_children[i]->i_children == -1 &&
118 p_root->pp_children[i]->p_input->i_id == i_input_id )
120 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
123 else if( p_root->pp_children[i]->i_children >= 0 )
125 int i_ret = playlist_DeleteFromInput( p_playlist, i_input_id,
126 p_root->pp_children[i], b_do_stop );
127 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
133 /** Remove a playlist item from the playlist, given its id */
134 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
136 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
137 if( !p_item ) return VLC_EGENERIC;
138 return DeleteInner( p_playlist, p_item, VLC_TRUE );
141 /** Remove a playlist item from the playlist, given its id
142 * This function should be entered without the playlist lock */
143 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
146 vlc_mutex_lock( &p_playlist->object_lock );
147 i_ret = playlist_DeleteFromItemId( p_playlist, i_id );
148 vlc_mutex_unlock( &p_playlist->object_lock );
152 /** Clear the playlist */
153 void playlist_Clear( playlist_t * p_playlist )
155 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
156 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
158 /** Clear the playlist. This function must be entered without the lock */
159 void playlist_LockClear( playlist_t *p_playlist )
161 vlc_mutex_lock( &p_playlist->object_lock );
162 playlist_Clear( p_playlist );
163 vlc_mutex_unlock( &p_playlist->object_lock );
166 /***************************************************************************
167 * Playlist item addition
168 ***************************************************************************/
169 /** Add an item to the playlist or the media library
170 * \param p_playlist the playlist to add into
171 * \param psz_uri the mrl to add to the playlist
172 * \param psz_name a text giving a name or description of this item
173 * \param i_mode the mode used when adding
174 * \param i_pos the position in the playlist where to add. If this is
175 * PLAYLIST_END the item will be added at the end of the playlist
176 * regardless of it's size
177 * \param b_playlist TRUE for playlist, FALSE for media library
178 * \return The id of the playlist item
180 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
181 const char *psz_name, int i_mode, int i_pos,
182 vlc_bool_t b_playlist )
184 return playlist_AddExt( p_playlist, psz_uri, psz_name,
185 i_mode, i_pos, -1, NULL, 0, b_playlist );
189 * Add a MRL into the playlist or the media library, duration and options given
191 * \param p_playlist the playlist to add into
192 * \param psz_uri the mrl to add to the playlist
193 * \param psz_name a text giving a name or description of this item
194 * \param i_mode the mode used when adding
195 * \param i_pos the position in the playlist where to add. If this is
196 * PLAYLIST_END the item will be added at the end of the playlist
197 * regardless of it's size
198 * \param i_duration length of the item in milliseconds.
199 * \param ppsz_options an array of options
200 * \param i_options the number of options
201 * \param b_playlist TRUE for playlist, FALSE for media library
202 * \return The id of the playlist item
204 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
205 const char *psz_name, int i_mode, int i_pos,
206 mtime_t i_duration, const char **ppsz_options,
207 int i_options, vlc_bool_t b_playlist )
209 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
210 i_options, ppsz_options,
213 return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist );
216 /** Add an input item to the playlist node */
217 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
218 int i_mode, int i_pos, vlc_bool_t b_playlist )
220 playlist_item_t *p_item_cat, *p_item_one;
222 if( !p_playlist->b_doing_ml )
223 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
226 vlc_mutex_lock( &p_playlist->object_lock );
228 /* Add to ONELEVEL */
229 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
230 if( p_item_one == NULL ) return VLC_EGENERIC;
231 AddItem( p_playlist, p_item_one,
232 b_playlist ? p_playlist->p_local_onelevel :
233 p_playlist->p_ml_onelevel , i_pos );
235 /* Add to CATEGORY */
236 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
237 if( p_item_cat == NULL ) return VLC_EGENERIC;
238 AddItem( p_playlist, p_item_cat,
239 b_playlist ? p_playlist->p_local_category :
240 p_playlist->p_ml_category , i_pos );
242 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
244 vlc_mutex_unlock( &p_playlist->object_lock );
248 /** Add an input item to p_direct_parent in the category tree, and to the
249 * matching top category in onelevel **/
250 int playlist_BothAddInput( playlist_t *p_playlist,
251 input_item_t *p_input,
252 playlist_item_t *p_direct_parent,
253 int i_mode, int i_pos )
255 playlist_item_t *p_item_cat, *p_item_one, *p_up;
258 vlc_mutex_lock( & p_playlist->object_lock );
260 /* Add to category */
261 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
262 if( p_item_cat == NULL ) return VLC_EGENERIC;
263 AddItem( p_playlist, p_item_cat, p_direct_parent, i_pos );
265 /* Add to onelevel */
266 /**Â \todo make a faster case for ml import */
267 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
268 if( p_item_one == NULL ) return VLC_EGENERIC;
270 p_up = p_direct_parent;
271 while( p_up->p_parent != p_playlist->p_root_category )
273 p_up = p_up->p_parent;
275 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
277 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id == p_up->p_input->i_id )
279 AddItem( p_playlist, p_item_one,
280 p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
284 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
286 vlc_mutex_unlock( &p_playlist->object_lock );
291 * Add an item where it should be added, when adding from a node
292 * (ex: directory access, playlist demuxers, services discovery, ... )
293 * \param p_playlist the playlist
294 * \param p_input the input to add
295 * \param p_parent the direct node
296 * \param p_item_in_category the item within category root (as returned by playlist_ItemToNode)
297 * \param b_forced_parent Are we forced to add only to p_parent ?
299 void playlist_AddWhereverNeeded( playlist_t *p_playlist, input_item_t *p_input,
300 playlist_item_t *p_parent,
301 playlist_item_t *p_item_in_category,
302 vlc_bool_t b_forced_parent, int i_mode )
304 /* If we have forced a parent :
305 * - Just add the input to the forced parent (which should be p_parent)
307 * - If we have item in category, add to it, and to onelevel (bothadd)
308 * - If we don't, just add to p_parent
310 if( b_forced_parent == VLC_TRUE || !p_item_in_category )
312 playlist_NodeAddInput( p_playlist, p_input, p_parent, i_mode,
317 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
318 i_mode, PLAYLIST_END );
323 /** Add an input item to a given node */
324 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
325 input_item_t *p_input,
326 playlist_item_t *p_parent,
327 int i_mode, int i_pos )
329 playlist_item_t *p_item;
331 assert( p_parent && p_parent->i_children != -1 );
333 vlc_mutex_lock( &p_playlist->object_lock );
335 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
336 if( p_item == NULL ) return NULL;
337 AddItem( p_playlist, p_item, p_parent, i_pos );
339 vlc_mutex_unlock( &p_playlist->object_lock );
344 /** Add a playlist item to a given node */
345 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
346 playlist_item_t *p_parent, int i_mode, int i_pos )
348 vlc_mutex_lock( &p_playlist->object_lock );
349 AddItem( p_playlist, p_item, p_parent, i_pos );
350 vlc_mutex_unlock( &p_playlist->object_lock );
353 /*****************************************************************************
354 * Playlist item misc operations
355 *****************************************************************************/
358 * Transform an item to a node. Return the node in the category tree, or NULL
360 * This function must be entered without the playlist lock
362 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
363 playlist_item_t *p_item )
366 playlist_item_t *p_item_in_category;
368 * Find the input in CATEGORY.
370 * - change it to node
371 * - we'll return it at the end
372 * - If we are a direct child of onelevel root, change to node, else
373 * delete the input from ONELEVEL
374 * - If we don't find it, just change to node (we are probably in VLM)
377 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
378 * useful for later BothAddInput )
381 /* Fast track the media library, no time to loose */
382 if( p_item == p_playlist->p_ml_category )
385 /** \todo First look if we don't already have it */
386 p_item_in_category = playlist_ItemFindFromInputAndRoot(
387 p_playlist, p_item->p_input->i_id,
388 p_playlist->p_root_category );
390 if( p_item_in_category )
392 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
393 p_playlist, p_item->p_input->i_id,
394 p_playlist->p_root_onelevel );
395 ChangeToNode( p_playlist, p_item_in_category );
396 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
397 ChangeToNode( p_playlist, p_item_in_one );
400 playlist_DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
401 p_playlist->p_root_onelevel, VLC_FALSE );
403 p_playlist->b_reset_random = VLC_TRUE;
404 var_SetInteger( p_playlist, "item-change", p_item_in_category->
406 return p_item_in_category;
410 ChangeToNode( p_playlist, p_item );
415 /** Transform an item to a node
416 * This function must be entered without the playlist lock
417 * \see playlist_ItemToNode
419 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
420 playlist_item_t *p_item )
422 playlist_item_t *p_ret;
423 vlc_mutex_lock( &p_playlist->object_lock );
424 p_ret = playlist_ItemToNode( p_playlist, p_item );
425 vlc_mutex_unlock( &p_playlist->object_lock );
429 /** Find an item within a root, given its input id.
430 * \return the first found item, or NULL if not found
432 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
434 playlist_item_t *p_root )
437 for( i = 0 ; i< p_root->i_children ; i++ )
439 if( p_root->pp_children[i]->i_children == -1 &&
440 p_root->pp_children[i]->p_input->i_id == i_input_id )
442 return p_root->pp_children[i];
444 else if( p_root->pp_children[i]->i_children >= 0 )
446 playlist_item_t *p_search =
447 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
448 p_root->pp_children[i] );
449 if( p_search ) return p_search;
459 * This function must be entered with the playlist lock
461 * \param p_playlist the playlist
462 * \param p_item the item to move
463 * \param p_node the new parent of the item
464 * \param i_newpos the new position under this new parent
465 * \return VLC_SUCCESS or an error
467 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
468 playlist_item_t *p_node, int i_newpos )
471 playlist_item_t *p_detach = NULL;
473 if( p_node->i_children == -1 ) return VLC_EGENERIC;
475 p_detach = p_item->p_parent;
476 for( j = 0; j < p_detach->i_children; j++ )
478 if( p_detach->pp_children[j] == p_item ) break;
480 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
482 /* Attach to new parent */
483 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
488 /** Send a notification that an item has been added to a node */
489 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
493 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
494 p_add->i_item = i_item_id;
495 p_add->i_node = i_node_id;
496 val.p_address = p_add;
497 p_playlist->b_reset_random = VLC_TRUE;
498 var_Set( p_playlist, "item-append", val );
502 /*****************************************************************************
503 * Playlist item accessors
504 *****************************************************************************/
506 /** Set the name of a playlist item */
507 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
509 if( psz_name && p_item )
511 if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
512 p_item->p_input->psz_name = strdup( psz_name );
518 /***************************************************************************
519 * The following functions are local
520 ***************************************************************************/
522 /* Enqueue an item for preparsing, and play it, if needed */
523 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
524 playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
526 if( (i_mode & PLAYLIST_GO ) )
528 playlist_item_t *p_parent = p_item_one;
529 playlist_item_t *p_toplay = NULL;
532 if( p_parent == p_playlist->p_root_category )
534 p_toplay = p_item_cat; break;
536 else if( p_parent == p_playlist->p_root_onelevel )
538 p_toplay = p_item_one; break;
540 p_parent = p_parent->p_parent;
543 p_playlist->request.b_request = VLC_TRUE;
544 p_playlist->request.p_item = p_toplay;
545 if( p_playlist->p_input )
547 input_StopThread( p_playlist->p_input );
549 p_playlist->request.i_status = PLAYLIST_RUNNING;
551 if( i_mode & PLAYLIST_PREPARSE &&
552 var_CreateGetBool( p_playlist, "auto-preparse" ) )
554 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
558 /* Add the playlist item to the requested node and fire a notification */
559 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
560 playlist_item_t *p_node, int i_pos )
562 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size,
563 p_playlist->i_size, p_item );
564 INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
565 p_playlist->i_all_size, p_item );
566 p_playlist->i_enabled ++;
568 if( i_pos == PLAYLIST_END )
570 playlist_NodeAppend( p_playlist, p_item, p_node );
574 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
576 if( !p_playlist->b_doing_ml )
577 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
580 /* Actually convert an item to a node */
581 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
584 if( p_item->i_children == -1 )
585 p_item->i_children = 0;
587 /* Remove it from the array of available items */
588 for( i = 0 ; i < p_playlist->i_size ; i++ )
590 if( p_item == p_playlist->pp_items[i] )
592 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
597 /* Do the actual removal */
598 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
601 int i, i_top, i_bottom;
602 int i_id = p_item->i_id;
603 vlc_bool_t b_flag = VLC_FALSE;
605 if( p_item->i_children > -1 )
607 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
609 p_playlist->b_reset_random = VLC_TRUE;
610 var_SetInteger( p_playlist, "item-deleted", i_id );
612 /* Remove the item from the bank */
613 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
615 while( p_playlist->pp_all_items[i]->i_id != i_id &&
618 if( p_playlist->pp_all_items[i]->i_id < i_id )
626 i = i_bottom + ( i_top - i_bottom ) / 2;
628 if( p_playlist->pp_all_items[i]->i_id == i_id )
630 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
633 /* Check if it is the current item */
634 if( p_playlist->status.p_item == p_item )
636 /* Hack we don't call playlist_Control for lock reasons */
639 p_playlist->request.i_status = PLAYLIST_STOPPED;
640 p_playlist->request.b_request = VLC_TRUE;
641 p_playlist->request.p_item = NULL;
642 msg_Info( p_playlist, "stopping playback" );
647 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
649 /* Remove the item from its parent */
650 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
652 if( b_flag == VLC_FALSE )
653 playlist_ItemDelete( p_item );
656 PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
657 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;