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 *vlc_input_item_GetInfo( 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 vlc_input_item_Destroy ( gc_object_t *p_this )
72 vlc_object_t *p_obj = (vlc_object_t *)p_this->p_destructor_arg;
74 input_item_t *p_input = (input_item_t *) p_this;
76 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_obj,
77 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
79 vlc_input_item_Clean( p_input );
84 i_bottom = 0; i_top = p_playlist->i_input_items - 1;
86 while( p_playlist->pp_input_items[i]->i_id != p_input->i_id &&
89 if( p_playlist->pp_input_items[i]->i_id < p_input->i_id )
94 i = i_bottom + ( i_top - i_bottom ) / 2;
97 if( p_playlist->pp_input_items[i]->i_id == p_input->i_id )
99 REMOVE_ELEM( p_playlist->pp_input_items,
100 p_playlist->i_input_items, i );
103 for( i = 0 ; i< p_playlist->i_input_items ; i++ )
105 if( p_playlist->pp_input_items[i]->i_id == p_input->i_id )
107 REMOVE_ELEM( p_playlist->pp_input_items,
108 p_playlist->i_input_items, i );
112 vlc_object_release( p_playlist );
117 int vlc_input_item_AddInfo( input_item_t *p_i,
119 const char *psz_name,
120 const char *psz_format, ... )
124 info_t *p_info = NULL;
125 info_category_t *p_cat = NULL ;
127 vlc_mutex_lock( &p_i->lock );
129 for( i = 0 ; i < p_i->i_categories ; i ++ )
131 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
133 p_cat = p_i->pp_categories[i];
139 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
141 vlc_mutex_unlock( &p_i->lock );
144 p_cat->psz_name = strdup( psz_cat );
147 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
151 for( i = 0; i< p_cat->i_infos; i++ )
153 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
155 p_info = p_cat->pp_infos[i];
162 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
164 vlc_mutex_unlock( &p_i->lock );
167 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
168 p_info->psz_name = strdup( psz_name );
172 if( p_info->psz_value ) free( p_info->psz_value );
175 va_start( args, psz_format );
176 vasprintf( &p_info->psz_value, psz_format, args);
179 vlc_mutex_unlock( &p_i->lock );
184 void vlc_input_item_AddOption( input_item_t *p_input,
185 const char *psz_option )
187 if( !psz_option ) return;
188 vlc_mutex_lock( &p_input->lock );
189 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
190 p_input->i_options, strdup( psz_option ) );
191 vlc_mutex_unlock( &p_input->lock );
195 input_item_t *input_ItemGetById( playlist_t *p_playlist, int i_id )
197 int i, i_top, i_bottom;
198 i_bottom = 0; i_top = p_playlist->i_input_items -1;
200 while( p_playlist->pp_input_items[i]->i_id != i_id &&
203 if( p_playlist->pp_input_items[i]->i_id < i_id )
207 i = i_bottom + ( i_top - i_bottom ) / 2;
209 if( p_playlist->pp_input_items[i]->i_id == i_id )
211 return p_playlist->pp_input_items[i];
216 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
217 const char *psz_name, int i_options,
218 const char **ppsz_options, int i_duration )
220 return input_ItemNewWithType( p_obj, psz_uri, psz_name,
221 i_options, ppsz_options,
222 i_duration, ITEM_TYPE_UNKNOWN );
226 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
227 const char *psz_name, int i_options,
228 const char **ppsz_options, int i_duration,
231 /* FIXME DON'T SEARCH PLAYLIST */
232 /* FIXME SHOULD LOCK */
233 input_item_t *p_input = (input_item_t *)malloc( sizeof( input_item_t ) );
234 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_obj,
235 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
237 vlc_input_item_Init( p_obj, p_input );
238 vlc_gc_init( p_input, vlc_input_item_Destroy, (void *)p_obj );
240 p_input->i_id = ++p_playlist->i_last_input_id;
242 INSERT_ELEM( p_playlist->pp_input_items, p_playlist->i_input_items,
243 p_playlist->i_input_items, p_input );
244 vlc_object_release( p_playlist );
246 p_input->b_fixed_name = VLC_FALSE;
249 p_input->psz_uri = strdup( psz_uri );
251 p_input->psz_uri = NULL;
253 if( psz_name != NULL )
254 p_input->psz_name = strdup( psz_name );
256 p_input->psz_name = strdup ( p_input->psz_uri );
258 p_input->i_type = i_type;
260 if( p_input->i_type == ITEM_TYPE_UNKNOWN )
261 GuessType( p_input );
263 p_input->i_duration = i_duration;
264 p_input->ppsz_options = NULL;
266 for( p_input->i_options = 0; p_input->i_options < i_options;
267 p_input->i_options++ )
269 if( !p_input->i_options )
271 p_input->ppsz_options = malloc( i_options * sizeof(char *) );
272 if( !p_input->ppsz_options ) break;
274 p_input->ppsz_options[p_input->i_options] =
275 strdup( ppsz_options[p_input->i_options] );
280 /* Guess the type of the item using the beginning of the mrl */
281 static void GuessType( input_item_t *p_item)
284 static struct { char *psz_search; int i_type; } types_array[] =
286 { "http", ITEM_TYPE_NET },
287 { "dvd", ITEM_TYPE_DISC },
288 { "cdda", ITEM_TYPE_CDDA },
289 { "mms", ITEM_TYPE_NET },
290 { "rtsp", ITEM_TYPE_NET },
291 { "udp", ITEM_TYPE_NET },
292 { "rtp", ITEM_TYPE_NET },
293 { "vcd", ITEM_TYPE_DISC },
294 { "v4l", ITEM_TYPE_CARD },
295 { "dshow", ITEM_TYPE_CARD },
296 { "pvr", ITEM_TYPE_CARD },
297 { "dvb", ITEM_TYPE_CARD },
298 { "qpsk", ITEM_TYPE_CARD },
299 { "sdp", ITEM_TYPE_NET },
304 static struct { char *psz_search; int i_type; } exts_array[] =
306 { "mp3", ITEM_TYPE_AFILE },
311 for( i = 0; types_array[i].psz_search != NULL; i++ )
313 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
314 strlen( types_array[i].psz_search ) ) )
316 p_item->i_type = types_array[i].i_type;
320 p_item->i_type = ITEM_TYPE_VFILE;