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 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name, p_input->psz_uri );
224 vlc_mutex_lock( &p_playlist->object_lock );
226 /* Add to ONELEVEL */
227 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
228 if( p_item_one == NULL ) return VLC_EGENERIC;
229 AddItem( p_playlist, p_item_one,
230 b_playlist ? p_playlist->p_local_onelevel :
231 p_playlist->p_ml_onelevel , i_pos );
233 /* Add to CATEGORY */
234 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
235 if( p_item_cat == NULL ) return VLC_EGENERIC;
236 AddItem( p_playlist, p_item_cat,
237 b_playlist ? p_playlist->p_local_category :
238 p_playlist->p_ml_category , i_pos );
240 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
242 vlc_mutex_unlock( &p_playlist->object_lock );
246 /** Add an input item to p_direct_parent in the category tree, and to the
247 * matching top category in onelevel **/
248 int playlist_BothAddInput( playlist_t *p_playlist,
249 input_item_t *p_input,
250 playlist_item_t *p_direct_parent,
251 int i_mode, int i_pos )
253 playlist_item_t *p_item_cat, *p_item_one, *p_up;
256 vlc_mutex_lock( & p_playlist->object_lock );
258 /* Add to category */
259 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
260 if( p_item_cat == NULL ) return VLC_EGENERIC;
261 AddItem( p_playlist, p_item_cat, p_direct_parent, i_pos );
263 /* Add to onelevel */
264 /**Â \todo make a faster case for ml import */
265 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
266 if( p_item_one == NULL ) return VLC_EGENERIC;
268 p_up = p_direct_parent;
269 while( p_up->p_parent != p_playlist->p_root_category )
271 p_up = p_up->p_parent;
273 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
275 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id == p_up->p_input->i_id )
277 AddItem( p_playlist, p_item_one,
278 p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
282 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
284 vlc_mutex_unlock( &p_playlist->object_lock );
289 * Add an item where it should be added, when adding from a node
290 * (ex: directory access, playlist demuxers, services discovery, ... )
291 * \param p_playlist the playlist
292 * \param p_input the input to add
293 * \param p_parent the direct node
294 * \param p_item_in_category the item within category root (as returned by playlist_ItemToNode)
295 * \param b_forced_parent Are we forced to add only to p_parent ?
297 void playlist_AddWhereverNeeded( playlist_t *p_playlist, input_item_t *p_input,
298 playlist_item_t *p_parent,
299 playlist_item_t *p_item_in_category,
300 vlc_bool_t b_forced_parent, int i_mode )
302 /* If we have forced a parent :
303 * - Just add the input to the forced parent (which should be p_parent)
305 * - If we have item in category, add to it, and to onelevel (bothadd)
306 * - If we don't, just add to p_parent
308 if( b_forced_parent == VLC_TRUE || !p_item_in_category )
310 playlist_NodeAddInput( p_playlist, p_input, p_parent, i_mode,
315 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
316 i_mode, PLAYLIST_END );
321 /** Add an input item to a given node */
322 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
323 input_item_t *p_input,
324 playlist_item_t *p_parent,
325 int i_mode, int i_pos )
327 playlist_item_t *p_item;
329 assert( p_parent && p_parent->i_children != -1 );
331 vlc_mutex_lock( &p_playlist->object_lock );
333 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
334 if( p_item == NULL ) return NULL;
335 AddItem( p_playlist, p_item, p_parent, i_pos );
337 vlc_mutex_unlock( &p_playlist->object_lock );
342 /** Add a playlist item to a given node */
343 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
344 playlist_item_t *p_parent, int i_mode, int i_pos )
346 vlc_mutex_lock( &p_playlist->object_lock );
347 AddItem( p_playlist, p_item, p_parent, i_pos );
348 vlc_mutex_unlock( &p_playlist->object_lock );
351 /*****************************************************************************
352 * Playlist item misc operations
353 *****************************************************************************/
356 * Transform an item to a node. Return the node in the category tree, or NULL
358 * This function must be entered without the playlist lock
360 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
361 playlist_item_t *p_item )
364 playlist_item_t *p_item_in_category;
366 * Find the input in CATEGORY.
368 * - change it to node
369 * - we'll return it at the end
370 * - If we are a direct child of onelevel root, change to node, else
371 * delete the input from ONELEVEL
372 * - If we don't find it, just change to node (we are probably in VLM)
375 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
376 * useful for later BothAddInput )
379 /* Fast track the media library, no time to loose */
380 if( p_item == p_playlist->p_ml_category )
383 /** \todo First look if we don't already have it */
384 p_item_in_category = playlist_ItemFindFromInputAndRoot(
385 p_playlist, p_item->p_input->i_id,
386 p_playlist->p_root_category );
388 if( p_item_in_category )
390 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
391 p_playlist, p_item->p_input->i_id,
392 p_playlist->p_root_onelevel );
393 ChangeToNode( p_playlist, p_item_in_category );
394 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
396 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 var_SetInteger( p_playlist, "item-change", p_item->p_input->i_id );
404 return p_item_in_category;
408 ChangeToNode( p_playlist, p_item );
413 /** Transform an item to a node
414 * This function must be entered without the playlist lock
415 * \see playlist_ItemToNode
417 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
418 playlist_item_t *p_item )
420 playlist_item_t *p_ret;
421 vlc_mutex_lock( &p_playlist->object_lock );
422 p_ret = playlist_ItemToNode( p_playlist, p_item );
423 vlc_mutex_unlock( &p_playlist->object_lock );
427 /** Find an item within a root, given its input id.
428 * \return the first found item, or NULL if not found
430 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
432 playlist_item_t *p_root )
435 for( i = 0 ; i< p_root->i_children ; i++ )
437 if( p_root->pp_children[i]->i_children == -1 &&
438 p_root->pp_children[i]->p_input->i_id == i_input_id )
440 return p_root->pp_children[i];
442 else if( p_root->pp_children[i]->i_children >= 0 )
444 playlist_item_t *p_search =
445 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
446 p_root->pp_children[i] );
447 if( p_search ) return p_search;
457 * This function must be entered with the playlist lock
459 * \param p_playlist the playlist
460 * \param p_item the item to move
461 * \param p_node the new parent of the item
462 * \param i_newpos the new position under this new parent
463 * \return VLC_SUCCESS or an error
465 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
466 playlist_item_t *p_node, int i_newpos )
469 playlist_item_t *p_detach = NULL;
471 if( p_node->i_children == -1 ) return VLC_EGENERIC;
473 p_detach = p_item->p_parent;
474 for( j = 0; j < p_detach->i_children; j++ )
476 if( p_detach->pp_children[j] == p_item ) break;
478 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
480 /* Attach to new parent */
481 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
486 /** Send a notification that an item has been added to a node */
487 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
491 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
492 p_add->i_item = i_item_id;
493 p_add->i_node = i_node_id;
494 val.p_address = p_add;
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, 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 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
521 playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
523 if( (i_mode & PLAYLIST_GO ) )
525 playlist_item_t *p_parent = p_item_one;
526 playlist_item_t *p_toplay = NULL;
529 if( p_parent == p_playlist->p_root_category )
531 p_toplay = p_item_cat; break;
533 else if( p_parent == p_playlist->p_root_onelevel )
535 p_toplay = p_item_one; break;
537 p_parent = p_parent->p_parent;
540 p_playlist->request.b_request = VLC_TRUE;
541 p_playlist->request.p_item = p_toplay;
542 if( p_playlist->p_input )
544 input_StopThread( p_playlist->p_input );
546 p_playlist->request.i_status = PLAYLIST_RUNNING;
548 if( i_mode & PLAYLIST_PREPARSE &&
549 var_CreateGetBool( p_playlist, "auto-preparse" ) )
551 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
555 /* Add the playlist item to the requested node and fire a notification */
556 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
557 playlist_item_t *p_node, int i_pos )
559 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size,
560 p_playlist->i_size, p_item );
561 INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
562 p_playlist->i_all_size, p_item );
563 p_playlist->i_enabled ++;
565 if( i_pos == PLAYLIST_END )
567 playlist_NodeAppend( p_playlist, p_item, p_node );
571 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
574 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
577 /* Actually convert an item to a node */
578 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
581 if( p_item->i_children == -1 )
582 p_item->i_children = 0;
584 /* Remove it from the array of available items */
585 for( i = 0 ; i < p_playlist->i_size ; i++ )
587 if( p_item == p_playlist->pp_items[i] )
589 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
594 /* Do the actual removal */
595 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
598 int i, i_top, i_bottom;
599 int i_id = p_item->i_id;
600 vlc_bool_t b_flag = VLC_FALSE;
602 if( p_item->i_children > -1 )
604 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
606 var_SetInteger( p_playlist, "item-deleted", i_id );
608 /* Remove the item from the bank */
609 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
611 while( p_playlist->pp_all_items[i]->i_id != i_id &&
614 if( p_playlist->pp_all_items[i]->i_id < i_id )
622 i = i_bottom + ( i_top - i_bottom ) / 2;
624 if( p_playlist->pp_all_items[i]->i_id == i_id )
626 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
629 /* Check if it is the current item */
630 if( p_playlist->status.p_item == p_item )
632 /* Hack we don't call playlist_Control for lock reasons */
635 p_playlist->status.i_status = PLAYLIST_STOPPED;
636 p_playlist->request.b_request = VLC_TRUE;
637 p_playlist->request.p_item = NULL;
638 msg_Info( p_playlist, "stopping playback" );
643 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
645 /* Remove the item from its parent */
646 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
648 if( b_flag == VLC_FALSE )
649 playlist_ItemDelete( p_item );
651 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;