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 *p_item );
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 if( psz_uri == NULL ) return NULL;
66 input_item_t *p_input = input_ItemNewWithType( p_obj, psz_uri,
67 psz_name, i_options, ppsz_options,
69 return playlist_ItemNewFromInput( p_obj, p_input );
72 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
73 input_item_t *p_input )
75 /** FIXME !!!!! don't find playlist each time */
76 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_obj, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
77 playlist_item_t * p_item;
78 p_item = malloc( sizeof( playlist_item_t ) );
79 if( p_item == NULL ) return NULL;
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 );
97 * Copy a playlist item - FIXME: Rewrite FIXME
99 * Creates a new item with name, mrl and meta infor like the
100 * source. Does not copy children for node type items.
101 * \param p_obj any vlc object, needed for mutex init
102 * \param p_item the item to copy
103 * \return pointer to the new item, or NULL on error
104 * \note function takes the lock on p_item
106 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
107 playlist_item_t *p_item )
109 playlist_item_t *p_res;
111 vlc_mutex_lock( &p_item->p_input->lock );
113 p_res = malloc( sizeof( playlist_item_t ) );
116 vlc_mutex_unlock( &p_item->p_input->lock );
121 vlc_mutex_init( p_obj, &p_res->p_input->lock );
123 if( p_item->p_input->i_options )
124 p_res->p_input->ppsz_options =
125 malloc( p_item->p_input->i_options * sizeof(char*) );
126 for( i = 0; i < p_item->p_input->i_options; i++ )
128 p_res->p_input->ppsz_options[i] = strdup( p_item->p_input->ppsz_options[i] );
131 if( p_item->i_children != -1 )
133 msg_Warn( p_obj, "not copying playlist-item's children" );
134 p_res->i_children = -1;
135 p_res->pp_children = NULL;
137 p_res->p_parent = NULL;
139 if( p_item->p_input->psz_name )
140 p_res->p_input->psz_name = strdup( p_item->p_input->psz_name );
141 if( p_item->p_input->psz_uri )
142 p_res->p_input->psz_uri = strdup( p_item->p_input->psz_uri );
144 if( p_item->p_input->i_es )
147 (es_format_t**)malloc( p_item->p_input->i_es * sizeof(es_format_t*));
148 for( i = 0; i < p_item->p_input->i_es; i++ )
150 p_res->p_input->es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
151 es_format_Copy( p_res->p_input->es[ i ],
152 p_item->p_input->es[ i ] );
155 if( p_item->p_input->i_categories )
157 p_res->p_input->pp_categories = NULL;
158 p_res->p_input->i_categories = 0;
159 for( i = 0; i < p_item->p_input->i_categories; i++ )
161 info_category_t *p_incat;
162 p_incat = p_item->p_input->pp_categories[i];
163 if( p_incat->i_infos )
166 for( j = 0; j < p_incat->i_infos; j++ )
168 vlc_input_item_AddInfo( p_res->p_input, p_incat->psz_name,
169 p_incat->pp_infos[j]->psz_name,
171 p_incat->pp_infos[j]->psz_value );
177 vlc_mutex_unlock( &p_item->p_input->lock );
181 /***************************************************************************
182 * Playlist item destruction
183 ***************************************************************************/
185 /** Delete a playlist item and detach its input item */
186 int playlist_ItemDelete( playlist_item_t *p_item )
188 vlc_gc_decref( p_item->p_input );
193 /** Remove an input item from ONELEVEL and CATEGORY */
194 int playlist_DeleteAllFromInput( playlist_t *p_playlist, int i_input_id )
196 playlist_DeleteFromInput( p_playlist, i_input_id,
197 p_playlist->p_root_category, VLC_TRUE );
198 playlist_DeleteFromInput( p_playlist, i_input_id,
199 p_playlist->p_root_onelevel, VLC_TRUE );
203 /** Remove an input item from ONELEVEL and CATEGORY.
204 * This function must be entered without the playlist lock */
205 int playlist_LockDeleteAllFromInput( playlist_t * p_playlist, int i_id )
208 vlc_mutex_lock( &p_playlist->object_lock );
209 i_ret = playlist_DeleteAllFromInput( p_playlist, i_id );
210 vlc_mutex_unlock( &p_playlist->object_lock );
214 /** Remove an input item when it appears from a root playlist item */
215 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
216 playlist_item_t *p_root, vlc_bool_t b_do_stop )
219 for( i = 0 ; i< p_root->i_children ; i++ )
221 if( p_root->pp_children[i]->i_children == -1 &&
222 p_root->pp_children[i]->p_input->i_id == i_input_id )
224 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
226 else if( p_root->pp_children[i]->i_children >= 0 )
228 return playlist_DeleteFromInput( p_playlist, i_input_id,
229 p_root->pp_children[i], b_do_stop );
234 /** Remove a playlist item from the playlist, given its id */
235 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
237 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
238 if( !p_item ) return VLC_EGENERIC;
239 return DeleteInner( p_playlist, p_item, VLC_TRUE );
242 /** Remove a playlist item from the playlist, given its id
243 * This function should be entered without the playlist lock */
244 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
247 vlc_mutex_lock( &p_playlist->object_lock );
248 i_ret = playlist_DeleteFromItemId( p_playlist, i_id );
249 vlc_mutex_unlock( &p_playlist->object_lock );
253 /** Clear the playlist */
254 void playlist_Clear( playlist_t * p_playlist )
256 playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
257 playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
259 /** Clear the playlist. This function must be entered without the lock */
260 void playlist_LockClear( playlist_t *p_playlist )
262 vlc_mutex_lock( &p_playlist->object_lock );
263 playlist_Clear( p_playlist );
264 vlc_mutex_unlock( &p_playlist->object_lock );
267 /***************************************************************************
268 * Playlist item addition
269 ***************************************************************************/
272 * Add a MRL into the playlist.
274 * \param p_playlist the playlist to add into
275 * \param psz_uri the mrl to add to the playlist
276 * \param psz_name a text giving a name or description of this item
277 * \param i_mode the mode used when adding
278 * \param i_pos the position in the playlist where to add. If this is
279 * PLAYLIST_END the item will be added at the end of the playlist
280 * regardless of it's size
281 * \return The id of the playlist item
283 int playlist_PlaylistAdd( playlist_t *p_playlist, const char *psz_uri,
284 const char *psz_name, int i_mode, int i_pos )
286 return playlist_PlaylistAddExt( p_playlist, psz_uri, psz_name,
287 i_mode, i_pos, -1, NULL, 0 );
291 * Add a MRL into the playlist, duration and options given
293 * \param p_playlist the playlist to add into
294 * \param psz_uri the mrl to add to the playlist
295 * \param psz_name a text giving a name or description of this item
296 * \param i_mode the mode used when adding
297 * \param i_pos the position in the playlist where to add. If this is
298 * PLAYLIST_END the item will be added at the end of the playlist
299 * regardless of it's size
300 * \param i_duration length of the item in milliseconds.
301 * \param ppsz_options an array of options
302 * \param i_options the number of options
303 * \return The id of the playlist item
305 int playlist_PlaylistAddExt( playlist_t *p_playlist, const char * psz_uri,
306 const char *psz_name, int i_mode, int i_pos,
307 mtime_t i_duration, const char **ppsz_options,
310 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
311 i_options, ppsz_options,
314 return playlist_PlaylistAddInput( p_playlist, p_input, i_mode, i_pos );
317 /** Add an input item to the playlist node */
318 int playlist_PlaylistAddInput( playlist_t* p_playlist, input_item_t *p_input,
319 int i_mode, int i_pos )
321 playlist_item_t *p_item;
322 p_input->i_id = ++p_playlist->i_last_input_id;
324 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
325 p_input->psz_name, p_input->psz_uri );
327 vlc_mutex_lock( &p_playlist->object_lock );
329 /* Add to ONELEVEL */
330 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
331 if( p_item == NULL ) return VLC_EGENERIC;
332 AddItem( p_playlist, p_item,p_playlist->p_local_onelevel, i_pos );
334 /* Add to CATEGORY */
335 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
336 if( p_item == NULL ) return VLC_EGENERIC;
337 AddItem( p_playlist, p_item, p_playlist->p_local_category, i_pos );
339 GoAndPreparse( p_playlist, i_mode, p_item );
341 vlc_mutex_unlock( &p_playlist->object_lock );
345 /** Add an input item to p_direct_parent in the category tree, and to the
346 * matching top category in onelevel **/
347 int playlist_BothAddInput( playlist_t *p_playlist,
348 input_item_t *p_input,
349 playlist_item_t *p_direct_parent,
350 int i_mode, int i_pos )
352 playlist_item_t *p_item, *p_up;
354 vlc_mutex_lock( & p_playlist->object_lock );
356 /* Add to category */
357 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
358 if( p_item == NULL ) return VLC_EGENERIC;
359 fprintf( stderr, "Adding to CATEGORY\n");
360 AddItem( p_playlist, p_item, p_direct_parent, i_pos );
362 /* Add to onelevel */
363 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
364 if( p_item == NULL ) return VLC_EGENERIC;
365 fprintf( stderr, "Adding to ONE\n");
367 p_up = p_direct_parent;
368 while( p_up->p_parent != p_playlist->p_root_category )
370 p_up = p_up->p_parent;
372 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
374 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id == p_up->p_input->i_id )
376 AddItem( p_playlist, p_item,
377 p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
381 GoAndPreparse( p_playlist, i_mode, p_item );
383 vlc_mutex_unlock( &p_playlist->object_lock );
388 * Add an item where it should be added, when adding from a node
389 * (ex: directory access, playlist demuxers, services discovery, ... )
390 * \param p_playlist the playlist
391 * \param p_input the input to add
392 * \param p_parent the direct node
393 * \param p_item_in_category the item within category root (as returned by playlist_ItemToNode)
394 * \param b_forced_parent Are we forced to add only to p_parent ?
396 void playlist_AddWhereverNeeded( playlist_t *p_playlist, input_item_t *p_input,
397 playlist_item_t *p_parent,
398 playlist_item_t *p_item_in_category,
399 vlc_bool_t b_forced_parent, int i_mode )
401 /* If we have forced a parent :
402 * - Just add the input to the forced parent (which should be p_parent)
404 * - If we have item in category, add to it, and to onelevel (bothadd)
405 * - If we don't, just add to p_parent
407 if( b_forced_parent == VLC_TRUE || !p_item_in_category )
409 playlist_NodeAddInput( p_playlist, p_input, p_parent, i_mode,
414 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
415 i_mode, PLAYLIST_END );
420 /** Add an input item to a given node */
421 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
422 input_item_t *p_input,
423 playlist_item_t *p_parent,
424 int i_mode, int i_pos )
426 playlist_item_t *p_item;
429 if( !p_parent || p_parent->i_children == -1 )
431 msg_Err( p_playlist, "invalid node" );
434 vlc_mutex_lock( &p_playlist->object_lock );
436 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
437 if( p_item == NULL ) return NULL;
438 AddItem( p_playlist, p_item, p_parent, i_pos );
440 vlc_mutex_unlock( &p_playlist->object_lock );
445 /** Add a playlist item to a given node */
446 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
447 playlist_item_t *p_parent, int i_mode, int i_pos )
449 vlc_mutex_lock( &p_playlist->object_lock );
450 AddItem( p_playlist, p_item, p_parent, i_pos );
451 vlc_mutex_unlock( &p_playlist->object_lock );
454 /*****************************************************************************
455 * Playlist item misc operations
456 *****************************************************************************/
459 * Transform an item to a node. Return the node in the category tree, or NULL
461 * This function must be entered without the playlist lock
463 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
464 playlist_item_t *p_item )
467 * Find the input in CATEGORY.
469 * - change it to node
470 * - we'll return it at the end
471 * - Delete the input from ONELEVEL
472 * - If we don't find it, just change to node (we are probably in VLM)
475 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
476 * useful for later BothAddInput )
479 /** \todo First look if we don't already have it */
480 playlist_item_t *p_item_in_category = playlist_ItemFindFromInputAndRoot(
481 p_playlist, p_item->p_input->i_id,
482 p_playlist->p_root_category );
484 if( p_item_in_category )
486 ChangeToNode( p_playlist, p_item_in_category );
487 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id,
488 p_playlist->p_root_onelevel, VLC_FALSE );
489 var_SetInteger( p_playlist, "item-change", p_item->p_input->i_id );
490 return p_item_in_category;
494 ChangeToNode( p_playlist, p_item );
499 /** Transform an item to a node
500 * This function must be entered without the playlist lock
501 * \see playlist_ItemToNode
503 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
504 playlist_item_t *p_item )
506 playlist_item_t *p_ret;
507 vlc_mutex_lock( &p_playlist->object_lock );
508 p_ret = playlist_ItemToNode( p_playlist, p_item );
509 vlc_mutex_unlock( &p_playlist->object_lock );
513 /** Find an item within a root, given its input id.
514 * \return the first found item, or NULL if not found
516 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
518 playlist_item_t *p_root )
521 for( i = 0 ; i< p_root->i_children ; i++ )
523 if( p_root->pp_children[i]->i_children == -1 &&
524 p_root->pp_children[i]->p_input->i_id == i_input_id )
526 return p_root->pp_children[i];
528 else if( p_root->pp_children[i]->i_children >= 0 )
530 playlist_item_t *p_search =
531 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
532 p_root->pp_children[i] );
533 if( p_search ) return p_search;
543 * This function must be entered with the playlist lock
545 * \param p_playlist the playlist
546 * \param p_item the item to move
547 * \param p_node the new parent of the item
548 * \param i_newpos the new position under this new parent
549 * \return VLC_SUCCESS or an error
551 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
552 playlist_item_t *p_node, int i_newpos )
555 playlist_item_t *p_detach = NULL;
557 if( p_node->i_children == -1 ) return VLC_EGENERIC;
559 p_detach = p_item->p_parent;
560 for( j = 0; j < p_detach->i_children; j++ )
562 if( p_detach->pp_children[j] == p_item ) break;
564 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
566 /* Attach to new parent */
567 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
572 /** Send a notification that an item has been added to a node */
573 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
577 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
578 p_add->i_item = i_item_id;
579 p_add->i_node = i_node_id;
580 val.p_address = p_add;
581 var_Set( p_playlist, "item-append", val );
585 /*****************************************************************************
586 * Playlist item accessors
587 *****************************************************************************/
589 /** Set the name of a playlist item */
590 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
592 if( psz_name && p_item )
594 if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
595 p_item->p_input->psz_name = strdup( psz_name );
601 /** Set the duration of a playlist item
602 * \param i_duration the new duration in microseconds
604 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
606 char psz_buffer[MSTRTIME_MAX_SIZE];
609 p_item->p_input->i_duration = i_duration;
610 if( i_duration != -1 )
612 secstotimestr( psz_buffer, (int)(i_duration/1000000) );
616 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
618 vlc_input_item_AddInfo( p_item->p_input, _("General") , _("Duration"),
626 /** Add an option to a playlist item */
627 void playlist_ItemAddOption( playlist_item_t *p_item,
628 const char *psz_option)
630 vlc_input_item_AddOption( p_item->p_input, psz_option );
633 /***************************************************************************
634 * The following functions are local
635 ***************************************************************************/
637 /* Enqueue an item for preparsing, and play it, if needed */
638 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
639 playlist_item_t *p_item )
642 if( (i_mode & PLAYLIST_GO ) && p_view )
644 p_playlist->request.b_request = VLC_TRUE;
646 p_playlist->request.i_view = VIEW_CATEGORY;
647 p_playlist->request.p_node = p_view->p_root;
648 p_playlist->request.p_item = p_item;
650 if( p_playlist->p_input )
652 input_StopThread( p_playlist->p_input );
654 p_playlist->status.i_status = PLAYLIST_RUNNING;
657 if( i_mode & PLAYLIST_PREPARSE &&
658 var_CreateGetBool( p_playlist, "auto-preparse" ) )
660 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
664 /* Add the playlist item to the requested node and fire a notification */
665 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
666 playlist_item_t *p_node, int i_pos )
668 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size,
669 p_playlist->i_size, p_item );
671 fprintf( stderr, "Adding input %s (id %i) - playlist item id %i "
672 "to node %s (id %i)\n",
673 p_item->p_input->psz_name, p_item->p_input->i_id,
674 p_item->i_id, p_node->p_input->psz_name,
677 INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
678 p_playlist->i_all_size, p_item );
679 p_playlist->i_enabled ++;
681 if( i_pos == PLAYLIST_END )
683 playlist_NodeAppend( p_playlist, p_item, p_node );
687 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
690 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
693 /* Actually convert an item to a node */
694 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
697 if( p_item->i_children == -1 )
698 p_item->i_children = 0;
700 /* Remove it from the array of available items */
701 for( i = 0 ; i < p_playlist->i_size ; i++ )
703 if( p_item == p_playlist->pp_items[i] )
705 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
710 /* Do the actual removal */
711 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
714 int i, i_top, i_bottom;
715 int i_id = p_item->i_id;
716 vlc_bool_t b_flag = VLC_FALSE;
718 //fprintf( stderr, "Deleting item %i - %s\n", i_id,
719 // p_item->p_input->psz_name );
721 if( p_item->i_children > -1 )
723 return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
725 var_SetInteger( p_playlist, "item-deleted", i_id );
727 /* Remove the item from the bank */
728 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
730 while( p_playlist->pp_all_items[i]->i_id != i_id &&
733 if( p_playlist->pp_all_items[i]->i_id < i_id )
741 i = i_bottom + ( i_top - i_bottom ) / 2;
743 if( p_playlist->pp_all_items[i]->i_id == i_id )
745 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
748 /* Check if it is the current item */
749 if( p_playlist->status.p_item == p_item )
751 /* Hack we don't call playlist_Control for lock reasons */
754 p_playlist->status.i_status = PLAYLIST_STOPPED;
755 p_playlist->request.b_request = VLC_TRUE;
756 p_playlist->request.p_item = NULL;
757 msg_Info( p_playlist, "stopping playback" );
762 msg_Dbg( p_playlist, "deleting playlist item `%s'",
763 p_item->p_input->psz_name );
765 /* Remove the item from its parent */
766 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
768 if( b_flag == VLC_FALSE )
769 playlist_ItemDelete( p_item );
771 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;