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 *****************************************************************************/
41 * Create a new item, without adding it to the playlist
43 * \param p_obj a vlc object (anyone will do)
44 * \param psz_uri the mrl of the item
45 * \param psz_name a text giving a name or description of the item
46 * \return the new item or NULL on failure
48 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
50 const char *psz_name )
52 return playlist_ItemNewWithType( p_obj, psz_uri,
53 psz_name, 0, NULL, -1,
57 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
61 const char **ppsz_options,
65 input_item_t *p_input;
67 if( psz_uri == NULL ) return NULL;
68 p_input = input_ItemNewWithType( p_obj, psz_uri,
69 psz_name, i_options, ppsz_options,
71 return playlist_ItemNewFromInput( p_obj, p_input );
74 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
75 input_item_t *p_input )
77 /** FIXME !!!!! don't find playlist each time */
78 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_obj, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
79 DECMALLOC_NULL( p_item, playlist_item_t );
81 p_item->p_input = p_input;
82 vlc_gc_incref( p_item->p_input );
84 p_item->i_id = ++p_playlist->i_last_playlist_id;
86 p_item->p_parent = NULL;
87 p_item->i_children = -1;
88 p_item->pp_children = NULL;
91 vlc_object_release( p_playlist );
96 /***************************************************************************
97 * Playlist item destruction
98 ***************************************************************************/
100 /** Delete a playlist item and detach its input item */
101 int playlist_ItemDelete( playlist_item_t *p_item )
103 vlc_gc_decref( p_item->p_input );
108 /** Remove an input item from ONELEVEL and CATEGORY */
109 int playlist_DeleteAllFromInput( playlist_t *p_playlist, int i_input_id )
111 playlist_DeleteFromInput( p_playlist, i_input_id,
112 p_playlist->p_root_category, VLC_TRUE );
113 playlist_DeleteFromInput( p_playlist, i_input_id,
114 p_playlist->p_root_onelevel, VLC_TRUE );
118 /** Remove an input item from ONELEVEL and CATEGORY.
119 * This function must be entered without the playlist lock */
120 int playlist_LockDeleteAllFromInput( playlist_t * p_playlist, int i_id )
123 vlc_mutex_lock( &p_playlist->object_lock );
124 i_ret = playlist_DeleteAllFromInput( p_playlist, i_id );
125 vlc_mutex_unlock( &p_playlist->object_lock );
129 /** Remove an input item when it appears from a root playlist item */
130 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
131 playlist_item_t *p_root, vlc_bool_t b_do_stop )
134 for( i = 0 ; i< p_root->i_children ; i++ )
136 if( p_root->pp_children[i]->i_children == -1 &&
137 p_root->pp_children[i]->p_input->i_id == i_input_id )
139 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
141 else if( p_root->pp_children[i]->i_children >= 0 )
143 return playlist_DeleteFromInput( p_playlist, i_input_id,
144 p_root->pp_children[i], b_do_stop );
150 /** Remove a playlist item from the playlist, given its id */
151 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
153 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
154 if( !p_item ) return VLC_EGENERIC;
155 return DeleteInner( p_playlist, p_item, VLC_TRUE );
158 /** Remove a playlist item from the playlist, given its id
159 * This function should be entered without the playlist lock */
160 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
163 vlc_mutex_lock( &p_playlist->object_lock );
164 i_ret = playlist_DeleteFromItemId( p_playlist, i_id );
165 vlc_mutex_unlock( &p_playlist->object_lock );
169 /** Clear the playlist */
170 void playlist_Clear( playlist_t * p_playlist )
172 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
173 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
175 /** Clear the playlist. This function must be entered without the lock */
176 void playlist_LockClear( playlist_t *p_playlist )
178 vlc_mutex_lock( &p_playlist->object_lock );
179 playlist_Clear( p_playlist );
180 vlc_mutex_unlock( &p_playlist->object_lock );
183 /***************************************************************************
184 * Playlist item addition
185 ***************************************************************************/
188 * Add a MRL into the playlist.
190 * \param p_playlist the playlist to add into
191 * \param psz_uri the mrl to add to the playlist
192 * \param psz_name a text giving a name or description of this item
193 * \param i_mode the mode used when adding
194 * \param i_pos the position in the playlist where to add. If this is
195 * PLAYLIST_END the item will be added at the end of the playlist
196 * regardless of it's size
197 * \return The id of the playlist item
199 int playlist_PlaylistAdd( playlist_t *p_playlist, const char *psz_uri,
200 const char *psz_name, int i_mode, int i_pos )
202 return playlist_PlaylistAddExt( p_playlist, psz_uri, psz_name,
203 i_mode, i_pos, -1, NULL, 0 );
207 * Add a MRL into the playlist, duration and options given
209 * \param p_playlist the playlist to add into
210 * \param psz_uri the mrl to add to the playlist
211 * \param psz_name a text giving a name or description of this item
212 * \param i_mode the mode used when adding
213 * \param i_pos the position in the playlist where to add. If this is
214 * PLAYLIST_END the item will be added at the end of the playlist
215 * regardless of it's size
216 * \param i_duration length of the item in milliseconds.
217 * \param ppsz_options an array of options
218 * \param i_options the number of options
219 * \return The id of the playlist item
221 int playlist_PlaylistAddExt( playlist_t *p_playlist, const char * psz_uri,
222 const char *psz_name, int i_mode, int i_pos,
223 mtime_t i_duration, const char **ppsz_options,
226 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
227 i_options, ppsz_options,
230 return playlist_PlaylistAddInput( p_playlist, p_input, i_mode, i_pos );
233 /** Add an input item to the playlist node */
234 int playlist_PlaylistAddInput( playlist_t* p_playlist, input_item_t *p_input,
235 int i_mode, int i_pos )
237 playlist_item_t *p_item_cat, *p_item_one;
239 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
240 p_input->psz_name, p_input->psz_uri );
242 vlc_mutex_lock( &p_playlist->object_lock );
244 /* Add to ONELEVEL */
245 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
246 if( p_item_one == NULL ) return VLC_EGENERIC;
247 AddItem( p_playlist, p_item_one ,p_playlist->p_local_onelevel, i_pos );
249 /* Add to CATEGORY */
250 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
251 if( p_item_cat == NULL ) return VLC_EGENERIC;
252 AddItem( p_playlist, p_item_cat, p_playlist->p_local_category, i_pos );
254 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
256 vlc_mutex_unlock( &p_playlist->object_lock );
260 /** Add an input item to p_direct_parent in the category tree, and to the
261 * matching top category in onelevel **/
262 int playlist_BothAddInput( playlist_t *p_playlist,
263 input_item_t *p_input,
264 playlist_item_t *p_direct_parent,
265 int i_mode, int i_pos )
267 playlist_item_t *p_item_cat, *p_item_one, *p_up;
270 vlc_mutex_lock( & p_playlist->object_lock );
272 /* Add to category */
273 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
274 if( p_item_cat == NULL ) return VLC_EGENERIC;
275 AddItem( p_playlist, p_item_cat, p_direct_parent, i_pos );
277 /* Add to onelevel */
278 /**Â \todo make a faster case for ml import */
279 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
280 if( p_item_one == NULL ) return VLC_EGENERIC;
282 p_up = p_direct_parent;
283 while( p_up->p_parent != p_playlist->p_root_category )
285 p_up = p_up->p_parent;
287 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
289 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id == p_up->p_input->i_id )
291 AddItem( p_playlist, p_item_one,
292 p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
296 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
298 vlc_mutex_unlock( &p_playlist->object_lock );
303 * Add an item where it should be added, when adding from a node
304 * (ex: directory access, playlist demuxers, services discovery, ... )
305 * \param p_playlist the playlist
306 * \param p_input the input to add
307 * \param p_parent the direct node
308 * \param p_item_in_category the item within category root (as returned by playlist_ItemToNode)
309 * \param b_forced_parent Are we forced to add only to p_parent ?
311 void playlist_AddWhereverNeeded( playlist_t *p_playlist, input_item_t *p_input,
312 playlist_item_t *p_parent,
313 playlist_item_t *p_item_in_category,
314 vlc_bool_t b_forced_parent, int i_mode )
316 /* If we have forced a parent :
317 * - Just add the input to the forced parent (which should be p_parent)
319 * - If we have item in category, add to it, and to onelevel (bothadd)
320 * - If we don't, just add to p_parent
322 if( b_forced_parent == VLC_TRUE || !p_item_in_category )
324 playlist_NodeAddInput( p_playlist, p_input, p_parent, i_mode,
329 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
330 i_mode, PLAYLIST_END );
335 /** Add an input item to a given node */
336 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
337 input_item_t *p_input,
338 playlist_item_t *p_parent,
339 int i_mode, int i_pos )
341 playlist_item_t *p_item;
343 assert( p_parent && p_parent->i_children != -1 );
345 vlc_mutex_lock( &p_playlist->object_lock );
347 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
348 if( p_item == NULL ) return NULL;
349 AddItem( p_playlist, p_item, p_parent, i_pos );
351 vlc_mutex_unlock( &p_playlist->object_lock );
356 /** Add a playlist item to a given node */
357 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
358 playlist_item_t *p_parent, int i_mode, int i_pos )
360 vlc_mutex_lock( &p_playlist->object_lock );
361 AddItem( p_playlist, p_item, p_parent, i_pos );
362 vlc_mutex_unlock( &p_playlist->object_lock );
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 )
378 * Find the input in CATEGORY.
380 * - change it to node
381 * - we'll return it at the end
382 * - If we are a direct child of onelevel root, change to node, else
383 * delete the input from ONELEVEL
384 * - If we don't find it, just change to node (we are probably in VLM)
387 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
388 * useful for later BothAddInput )
391 /* Fast track the media library, no time to loose */
392 if( p_item == p_playlist->p_ml_category )
395 /** \todo First look if we don't already have it */
396 playlist_item_t *p_item_in_category = playlist_ItemFindFromInputAndRoot(
397 p_playlist, p_item->p_input->i_id,
398 p_playlist->p_root_category );
400 if( p_item_in_category )
402 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
403 p_playlist, p_item->p_input->i_id,
404 p_playlist->p_root_onelevel );
405 ChangeToNode( p_playlist, p_item_in_category );
406 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
408 ChangeToNode( p_playlist, p_item_in_one );
412 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id,
413 p_playlist->p_root_onelevel, VLC_FALSE );
415 var_SetInteger( p_playlist, "item-change", p_item->p_input->i_id );
416 return p_item_in_category;
420 ChangeToNode( p_playlist, p_item );
425 /** Transform an item to a node
426 * This function must be entered without the playlist lock
427 * \see playlist_ItemToNode
429 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
430 playlist_item_t *p_item )
432 playlist_item_t *p_ret;
433 vlc_mutex_lock( &p_playlist->object_lock );
434 p_ret = playlist_ItemToNode( p_playlist, p_item );
435 vlc_mutex_unlock( &p_playlist->object_lock );
439 /** Find an item within a root, given its input id.
440 * \return the first found item, or NULL if not found
442 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
444 playlist_item_t *p_root )
447 for( i = 0 ; i< p_root->i_children ; i++ )
449 if( p_root->pp_children[i]->i_children == -1 &&
450 p_root->pp_children[i]->p_input->i_id == i_input_id )
452 return p_root->pp_children[i];
454 else if( p_root->pp_children[i]->i_children >= 0 )
456 playlist_item_t *p_search =
457 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
458 p_root->pp_children[i] );
459 if( p_search ) return p_search;
469 * This function must be entered with the playlist lock
471 * \param p_playlist the playlist
472 * \param p_item the item to move
473 * \param p_node the new parent of the item
474 * \param i_newpos the new position under this new parent
475 * \return VLC_SUCCESS or an error
477 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
478 playlist_item_t *p_node, int i_newpos )
481 playlist_item_t *p_detach = NULL;
483 if( p_node->i_children == -1 ) return VLC_EGENERIC;
485 p_detach = p_item->p_parent;
486 for( j = 0; j < p_detach->i_children; j++ )
488 if( p_detach->pp_children[j] == p_item ) break;
490 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
492 /* Attach to new parent */
493 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
498 /** Send a notification that an item has been added to a node */
499 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
503 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
504 p_add->i_item = i_item_id;
505 p_add->i_node = i_node_id;
506 val.p_address = p_add;
507 var_Set( p_playlist, "item-append", val );
511 /*****************************************************************************
512 * Playlist item accessors
513 *****************************************************************************/
515 /** Set the name of a playlist item */
516 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
518 if( psz_name && p_item )
520 if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
521 p_item->p_input->psz_name = strdup( psz_name );
527 /** Set the duration of a playlist item
528 * \param i_duration the new duration in microseconds
530 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
532 char psz_buffer[MSTRTIME_MAX_SIZE];
535 p_item->p_input->i_duration = i_duration;
536 if( i_duration != -1 )
538 secstotimestr( psz_buffer, (int)(i_duration/1000000) );
542 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
544 vlc_input_item_AddInfo( p_item->p_input, _("General") , _("Duration"),
552 /** Add an option to a playlist item */
553 void playlist_ItemAddOption( playlist_item_t *p_item,
554 const char *psz_option)
556 vlc_input_item_AddOption( p_item->p_input, psz_option );
559 /***************************************************************************
560 * The following functions are local
561 ***************************************************************************/
563 /* Enqueue an item for preparsing, and play it, if needed */
564 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
565 playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
567 if( (i_mode & PLAYLIST_GO ) )
569 playlist_item_t *p_parent = p_item_one;
570 playlist_item_t *p_toplay = NULL;
573 if( p_parent == p_playlist->p_root_category )
575 p_toplay = p_item_cat; break;
577 else if( p_parent == p_playlist->p_root_onelevel )
579 p_toplay = p_item_one; break;
581 p_parent = p_parent->p_parent;
584 p_playlist->request.b_request = VLC_TRUE;
585 p_playlist->request.p_item = p_toplay;
586 if( p_playlist->p_input )
588 input_StopThread( p_playlist->p_input );
590 p_playlist->request.i_status = PLAYLIST_RUNNING;
592 if( i_mode & PLAYLIST_PREPARSE &&
593 var_CreateGetBool( p_playlist, "auto-preparse" ) )
595 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
599 /* Add the playlist item to the requested node and fire a notification */
600 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
601 playlist_item_t *p_node, int i_pos )
603 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size,
604 p_playlist->i_size, p_item );
606 fprintf( stderr, "Adding input %s (id %i) - playlist item id %i "
607 "to node %s (id %i)\n",
608 p_item->p_input->psz_name, p_item->p_input->i_id,
609 p_item->i_id, p_node->p_input->psz_name,
612 INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
613 p_playlist->i_all_size, p_item );
614 p_playlist->i_enabled ++;
616 if( i_pos == PLAYLIST_END )
618 playlist_NodeAppend( p_playlist, p_item, p_node );
622 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
625 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
628 /* Actually convert an item to a node */
629 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
632 if( p_item->i_children == -1 )
633 p_item->i_children = 0;
635 /* Remove it from the array of available items */
636 for( i = 0 ; i < p_playlist->i_size ; i++ )
638 if( p_item == p_playlist->pp_items[i] )
640 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
645 /* Do the actual removal */
646 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
649 int i, i_top, i_bottom;
650 int i_id = p_item->i_id;
651 vlc_bool_t b_flag = VLC_FALSE;
653 //fprintf( stderr, "Deleting item %i - %s\n", i_id,
654 // p_item->p_input->psz_name );
656 if( p_item->i_children > -1 )
658 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
660 var_SetInteger( p_playlist, "item-deleted", i_id );
662 /* Remove the item from the bank */
663 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
665 while( p_playlist->pp_all_items[i]->i_id != i_id &&
668 if( p_playlist->pp_all_items[i]->i_id < i_id )
676 i = i_bottom + ( i_top - i_bottom ) / 2;
678 if( p_playlist->pp_all_items[i]->i_id == i_id )
680 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
683 /* Check if it is the current item */
684 if( p_playlist->status.p_item == p_item )
686 /* Hack we don't call playlist_Control for lock reasons */
689 p_playlist->status.i_status = PLAYLIST_STOPPED;
690 p_playlist->request.b_request = VLC_TRUE;
691 p_playlist->request.p_item = NULL;
692 msg_Info( p_playlist, "stopping playback" );
697 msg_Dbg( p_playlist, "deleting playlist item `%s'",
698 p_item->p_input->psz_name );
700 /* Remove the item from its parent */
701 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
703 if( b_flag == VLC_FALSE )
704 playlist_ItemDelete( p_item );
706 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;