1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
28 #include <vlc/input.h>
30 #include "vlc_input.h"
31 #include "vlc_playlist.h"
33 static void GuessType( input_item_t *p_item);
36 * Create a new item, without adding it to the playlist
38 * \param p_obj a vlc object (anyone will do)
39 * \param psz_uri the mrl of the item
40 * \param psz_name a text giving a name or description of the item
41 * \return the new item or NULL on failure
44 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
46 const char *psz_name )
48 return playlist_ItemNewWithType( p_obj, psz_uri,
49 psz_name, ITEM_TYPE_UNKNOWN );
52 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
57 playlist_item_t * p_item;
59 if( psz_uri == NULL) return NULL;
61 p_item = malloc( sizeof( playlist_item_t ) );
62 if( p_item == NULL ) return NULL;
64 memset( p_item, 0, sizeof( playlist_item_t ) );
66 vlc_input_item_Init( p_obj, &p_item->input );
67 p_item->input.b_fixed_name = VLC_FALSE;
69 p_item->input.psz_uri = strdup( psz_uri );
71 if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
72 else p_item->input.psz_name = strdup ( psz_uri );
74 p_item->input.i_type = i_type;
76 p_item->b_enabled = VLC_TRUE;
77 p_item->i_nb_played = 0;
79 p_item->i_children = -1;
80 p_item->pp_children = NULL;
83 p_item->i_flags |= PLAYLIST_SKIP_FLAG;
84 p_item->i_flags |= PLAYLIST_SAVE_FLAG;
86 p_item->input.i_duration = -1;
87 p_item->input.ppsz_options = NULL;
88 p_item->input.i_options = 0;
90 vlc_mutex_init( p_obj, &p_item->input.lock );
92 if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
93 GuessType( &p_item->input );
99 * Copy a playlist item
101 * Creates a new item with name, mrl and meta infor like the
102 * source. Does not copy children for node type items.
103 * \param p_obj any vlc object, needed for mutex init
104 * \param p_item the item to copy
105 * \return pointer to the new item, or NULL on error
106 * \note function takes the lock on p_item
108 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
109 playlist_item_t *p_item )
111 playlist_item_t *p_res;
113 vlc_mutex_lock( &p_item->input.lock );
115 p_res = malloc( sizeof( playlist_item_t ) );
118 vlc_mutex_unlock( &p_item->input.lock );
123 vlc_mutex_init( p_obj, &p_res->input.lock );
125 if( p_item->input.i_options )
126 p_res->input.ppsz_options =
127 malloc( p_item->input.i_options * sizeof(char*) );
128 for( i = 0; i < p_item->input.i_options; i++ )
130 p_res->input.ppsz_options[i] = strdup( p_item->input.ppsz_options[i] );
133 if( p_item->i_children != -1 )
135 msg_Warn( p_obj, "not copying playlist items children" );
136 p_res->i_children = -1;
137 p_res->pp_children = NULL;
139 p_res->i_parents = 0;
140 p_res->pp_parents = NULL;
142 if( p_item->input.psz_name )
143 p_res->input.psz_name = strdup( p_item->input.psz_name );
144 if( p_item->input.psz_uri )
145 p_res->input.psz_uri = strdup( p_item->input.psz_uri );
147 if( p_item->input.i_es )
150 (es_format_t**)malloc( p_item->input.i_es * sizeof(es_format_t*));
151 for( i = 0; i < p_item->input.i_es; i++ )
153 p_res->input.es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
154 es_format_Copy( p_res->input.es[ i ],
155 p_item->input.es[ i ] );
158 if( p_item->input.i_categories )
160 p_res->input.pp_categories = NULL;
161 p_res->input.i_categories = 0;
162 for( i = 0; i < p_item->input.i_categories; i++ )
164 info_category_t *p_incat;
165 p_incat = p_item->input.pp_categories[i];
166 if( p_incat->i_infos )
169 for( j = 0; j < p_incat->i_infos; j++ )
171 vlc_input_item_AddInfo( &p_res->input, p_incat->psz_name,
172 p_incat->pp_infos[j]->psz_name,
173 "%s", /* to be safe */
174 p_incat->pp_infos[j]->psz_value );
180 vlc_mutex_unlock( &p_item->input.lock );
185 * Deletes a playlist item
187 * \param p_item the item to delete
190 int playlist_ItemDelete( playlist_item_t *p_item )
192 vlc_mutex_lock( &p_item->input.lock );
194 if( p_item->input.psz_name ) free( p_item->input.psz_name );
195 if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
197 /* Free the info categories */
198 if( p_item->input.i_categories > 0 )
202 for( i = 0; i < p_item->input.i_categories; i++ )
204 info_category_t *p_category = p_item->input.pp_categories[i];
206 for( j = 0; j < p_category->i_infos; j++)
208 if( p_category->pp_infos[j]->psz_name )
210 free( p_category->pp_infos[j]->psz_name);
212 if( p_category->pp_infos[j]->psz_value )
214 free( p_category->pp_infos[j]->psz_value );
216 free( p_category->pp_infos[j] );
219 if( p_category->i_infos ) free( p_category->pp_infos );
220 if( p_category->psz_name ) free( p_category->psz_name );
224 free( p_item->input.pp_categories );
227 for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
229 free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
230 if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
233 for( ; p_item->i_parents > 0 ; )
235 struct item_parent_t *p_parent = p_item->pp_parents[0];
236 REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
240 vlc_mutex_unlock( &p_item->input.lock );
241 vlc_mutex_destroy( &p_item->input.lock );
249 * Add a option to one item ( no need for p_playlist )
251 * \param p_item the item on which we want the info
252 * \param psz_option the option
253 * \return 0 on success
255 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
257 if( !psz_option ) return VLC_EGENERIC;
259 vlc_mutex_lock( &p_item->input.lock );
260 INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
261 p_item->input.i_options, strdup( psz_option ) );
262 vlc_mutex_unlock( &p_item->input.lock );
268 * Add a parent to an item
270 * \param p_item the item
271 * \param i_view the view in which the parent is
272 * \param p_parent the parent to add
275 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
276 playlist_item_t *p_parent )
278 vlc_bool_t b_found = VLC_FALSE;
281 for( i= 0; i< p_item->i_parents ; i++ )
283 if( p_item->pp_parents[i]->i_view == i_view )
290 if( b_found == VLC_FALSE )
293 struct item_parent_t *p_ip = (struct item_parent_t *)
294 malloc(sizeof(struct item_parent_t) );
295 p_ip->i_view = i_view;
296 p_ip->p_parent = p_parent;
298 INSERT_ELEM( p_item->pp_parents,
299 p_item->i_parents, p_item->i_parents,
306 * Copy all parents from parent to child
308 int playlist_CopyParents( playlist_item_t *p_parent,
309 playlist_item_t *p_child )
312 for( i= 0 ; i< p_parent->i_parents; i ++ )
314 playlist_ItemAddParent( p_child,
315 p_parent->pp_parents[i]->i_view,
322 /**********************************************************************
323 * playlist_item_t structure accessors
324 * These functions give access to the fields of the playlist_item_t
326 **********************************************************************/
330 * Set the name of a playlist item
332 * \param p_item the item
333 * \param psz_name the new name
334 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
336 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
338 if( psz_name && p_item )
340 if( p_item->input.psz_name ) free( p_item->input.psz_name );
341 p_item->input.psz_name = strdup( psz_name );
348 * Set the duration of a playlist item
349 * This function must be entered with the item lock
351 * \param p_item the item
352 * \param i_duration the new duration
353 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
355 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
357 char psz_buffer[MSTRTIME_MAX_SIZE];
360 p_item->input.i_duration = i_duration;
361 if( i_duration != -1 )
363 secstotimestr( psz_buffer, (int)(i_duration/1000000) );
367 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
369 vlc_input_item_AddInfo( &p_item->input, _("General") , _("Duration"),
378 * Guess the type of the item using the beginning of the mrl */
379 static void GuessType( input_item_t *p_item)
382 static struct { char *psz_search; int i_type; } types_array[] =
384 { "http", ITEM_TYPE_NET },
385 { "dvd", ITEM_TYPE_DISC },
386 { "cdda", ITEM_TYPE_CDDA },
387 { "mms", ITEM_TYPE_NET },
388 { "rtsp", ITEM_TYPE_NET },
389 { "udp", ITEM_TYPE_NET },
390 { "rtp", ITEM_TYPE_NET },
391 { "vcd", ITEM_TYPE_DISC },
392 { "v4l", ITEM_TYPE_CARD },
393 { "dshow", ITEM_TYPE_CARD },
394 { "pvr", ITEM_TYPE_CARD },
395 { "dvb", ITEM_TYPE_CARD },
396 { "qpsk", ITEM_TYPE_CARD },
397 { "sdp", ITEM_TYPE_NET },
402 static struct { char *psz_search; int i_type; } exts_array[] =
404 { "mp3", ITEM_TYPE_AFILE },
409 for( i = 0; types_array[i].psz_search != NULL; i++ )
411 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
412 strlen( types_array[i].psz_search ) ) )
414 p_item->i_type = types_array[i].i_type;
418 p_item->i_type = ITEM_TYPE_VFILE;