1 /*****************************************************************************
2 * item.c: input_item management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.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 *****************************************************************************/
25 #include <vlc/input.h>
26 #include "vlc_playlist.h"
27 #include "vlc_interface.h"
29 static void GuessType( input_item_t *p_item );
32 * Get a info item from a given category in a given input item.
34 * \param p_i The input item to get info from
35 * \param psz_cat String representing the category for the info
36 * \param psz_name String representing the name of the desired info
37 * \return A pointer to the string with the given info if found, or an
38 * empty string otherwise. The caller should free the returned
41 char *input_ItemGetInfo( input_item_t *p_i,
43 const char *psz_name )
47 vlc_mutex_lock( &p_i->lock );
49 for( i = 0 ; i< p_i->i_categories ; i++ )
51 info_category_t *p_cat = p_i->pp_categories[i];
53 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
56 for( j = 0; j < p_cat->i_infos ; j++ )
58 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
60 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
61 vlc_mutex_unlock( &p_i->lock );
66 vlc_mutex_unlock( &p_i->lock );
70 static void input_ItemDestroy ( gc_object_t *p_this )
72 vlc_object_t *p_obj = (vlc_object_t *)p_this->p_destructor_arg;
73 input_item_t *p_input = (input_item_t *) p_this;
76 playlist_t *p_playlist = pl_Yield( p_obj );
77 input_ItemClean( p_input );
79 ARRAY_BSEARCH( p_playlist->input_items,->i_id, int, p_input->i_id, i);
81 ARRAY_REMOVE( p_playlist->input_items, i);
87 void input_ItemAddOption( input_item_t *p_input,
88 const char *psz_option )
90 if( !psz_option ) return;
91 vlc_mutex_lock( &p_input->lock );
92 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
93 p_input->i_options, strdup( psz_option ) );
94 vlc_mutex_unlock( &p_input->lock );
97 void input_ItemAddOptionNoDup( input_item_t *p_input,
98 const char *psz_option )
101 if( !psz_option ) return ;
102 vlc_mutex_lock( &p_input->lock );
103 for( i = 0 ; i< p_input->i_options; i++ )
105 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
107 vlc_mutex_unlock(& p_input->lock );
111 TAB_APPEND( p_input->i_options, p_input->ppsz_options, strdup( psz_option)); vlc_mutex_unlock( &p_input->lock );
115 int input_ItemAddInfo( input_item_t *p_i,
117 const char *psz_name,
118 const char *psz_format, ... )
122 info_t *p_info = NULL;
123 info_category_t *p_cat = NULL ;
125 vlc_mutex_lock( &p_i->lock );
127 for( i = 0 ; i < p_i->i_categories ; i ++ )
129 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
131 p_cat = p_i->pp_categories[i];
137 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
139 vlc_mutex_unlock( &p_i->lock );
142 p_cat->psz_name = strdup( psz_cat );
145 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
149 for( i = 0; i< p_cat->i_infos; i++ )
151 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
153 p_info = p_cat->pp_infos[i];
160 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
162 vlc_mutex_unlock( &p_i->lock );
165 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
166 p_info->psz_name = strdup( psz_name );
170 if( p_info->psz_value ) free( p_info->psz_value );
173 va_start( args, psz_format );
174 vasprintf( &p_info->psz_value, psz_format, args);
177 vlc_mutex_unlock( &p_i->lock );
182 input_item_t *input_ItemGetById( playlist_t *p_playlist, int i_id )
185 ARRAY_BSEARCH( p_playlist->input_items, ->i_id, int, i_id, i);
187 return ARRAY_VAL( p_playlist->input_items, i);
191 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
192 const char *psz_name, int i_options,
193 const char *const *ppsz_options, int i_duration )
195 return input_ItemNewWithType( p_obj, psz_uri, psz_name,
196 i_options, ppsz_options,
197 i_duration, ITEM_TYPE_UNKNOWN );
201 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
202 const char *psz_name, int i_options,
203 const char *const *ppsz_options, int i_duration,
206 playlist_t *p_playlist = pl_Yield( p_obj );
207 DECMALLOC_NULL( p_input, input_item_t );
209 input_ItemInit( p_obj, p_input );
210 vlc_gc_init( p_input, input_ItemDestroy, (void *)p_obj );
213 p_input->i_id = ++p_playlist->i_last_input_id;
214 ARRAY_APPEND( p_playlist->input_items, p_input );
218 p_input->b_fixed_name = VLC_FALSE;
221 p_input->psz_uri = strdup( psz_uri );
223 p_input->psz_uri = NULL;
225 p_input->i_type = i_type;
226 p_input->b_prefers_tree = VLC_FALSE;
228 if( p_input->i_type == ITEM_TYPE_UNKNOWN )
229 GuessType( p_input );
231 if( psz_name != NULL )
232 p_input->psz_name = strdup( psz_name );
233 else if( p_input->i_type == ITEM_TYPE_AFILE
234 || p_input->i_type == ITEM_TYPE_VFILE )
236 const char *psz_filename = strrchr( p_input->psz_uri, DIR_SEP_CHAR );
237 if( psz_filename && *psz_filename == DIR_SEP_CHAR )
239 p_input->psz_name = strdup( psz_filename && *psz_filename
240 ? psz_filename : p_input->psz_uri );
243 p_input->psz_name = strdup( p_input->psz_uri );
245 p_input->i_duration = i_duration;
246 p_input->ppsz_options = NULL;
248 for( p_input->i_options = 0; p_input->i_options < i_options;
249 p_input->i_options++ )
251 if( !p_input->i_options )
253 p_input->ppsz_options = malloc( i_options * sizeof(char *) );
254 if( !p_input->ppsz_options ) break;
256 p_input->ppsz_options[p_input->i_options] =
257 strdup( ppsz_options[p_input->i_options] );
262 /* Guess the type of the item using the beginning of the mrl */
263 static void GuessType( input_item_t *p_item)
266 static struct { const char *psz_search; int i_type; } types_array[] =
268 { "http", ITEM_TYPE_NET },
269 { "dvd", ITEM_TYPE_DISC },
270 { "cdda", ITEM_TYPE_CDDA },
271 { "mms", ITEM_TYPE_NET },
272 { "rtsp", ITEM_TYPE_NET },
273 { "udp", ITEM_TYPE_NET },
274 { "rtp", ITEM_TYPE_NET },
275 { "vcd", ITEM_TYPE_DISC },
276 { "v4l", ITEM_TYPE_CARD },
277 { "dshow", ITEM_TYPE_CARD },
278 { "pvr", ITEM_TYPE_CARD },
279 { "dvb", ITEM_TYPE_CARD },
280 { "qpsk", ITEM_TYPE_CARD },
281 { "sdp", ITEM_TYPE_NET },
286 static struct { char *psz_search; int i_type; } exts_array[] =
288 { "mp3", ITEM_TYPE_AFILE },
293 for( i = 0; types_array[i].psz_search != NULL; i++ )
295 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
296 strlen( types_array[i].psz_search ) ) )
298 p_item->i_type = types_array[i].i_type;
302 p_item->i_type = ITEM_TYPE_VFILE;