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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include "vlc_playlist.h"
31 #include "vlc_interface.h"
33 #include "input_internal.h"
35 static void GuessType( input_item_t *p_item );
37 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
39 static inline void input_ItemInit( vlc_object_t *p_o, input_item_t *p_i )
41 memset( p_i, 0, sizeof(input_item_t) );
44 TAB_INIT( p_i->i_es, p_i->es );
45 TAB_INIT( p_i->i_options, p_i->ppsz_options );
46 p_i->optflagv = NULL, p_i->optflagc = 0;
47 TAB_INIT( p_i->i_categories, p_i->pp_categories );
49 p_i->i_type = ITEM_TYPE_UNKNOWN;
50 p_i->b_fixed_name = true;
55 vlc_mutex_init( &p_i->lock );
56 vlc_event_manager_init( &p_i->event_manager, p_i, p_o );
57 vlc_event_manager_register_event_type( &p_i->event_manager,
58 vlc_InputItemMetaChanged );
59 vlc_event_manager_register_event_type( &p_i->event_manager,
60 vlc_InputItemSubItemAdded );
61 vlc_event_manager_register_event_type( &p_i->event_manager,
62 vlc_InputItemDurationChanged );
63 vlc_event_manager_register_event_type( &p_i->event_manager,
64 vlc_InputItemPreparsedChanged );
67 static inline void input_ItemClean( input_item_t *p_i )
71 vlc_event_manager_fini( &p_i->event_manager );
73 free( p_i->psz_name );
77 vlc_mutex_destroy( &p_i->p_stats->lock );
82 vlc_meta_Delete( p_i->p_meta );
84 for( i = 0; i < p_i->i_options; i++ )
85 free( p_i->ppsz_options[i] );
86 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
89 for( i = 0; i < p_i->i_es; i++ )
91 es_format_Clean( p_i->es[i] );
94 TAB_CLEAN( p_i->i_es, p_i->es );
96 for( i = 0; i < p_i->i_categories; i++ )
98 info_category_t *p_category = p_i->pp_categories[i];
101 for( j = 0; j < p_category->i_infos; j++ )
103 struct info_t *p_info = p_category->pp_infos[j];
105 free( p_info->psz_name);
106 free( p_info->psz_value );
109 TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
111 free( p_category->psz_name );
114 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
116 vlc_mutex_destroy( &p_i->lock );
119 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
123 vlc_mutex_lock( &p_i->lock );
125 p_i->p_meta = vlc_meta_New();
126 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
127 vlc_mutex_unlock( &p_i->lock );
129 /* Notify interested third parties */
130 event.type = vlc_InputItemMetaChanged;
131 event.u.input_item_meta_changed.meta_type = meta_type;
132 vlc_event_send( &p_i->event_manager, &event );
136 * Get the item from an input thread
138 input_item_t *input_GetItem( input_thread_t *p_input )
140 assert( p_input && p_input->p );
141 return p_input->p->input.p_item;
145 * Get a info item from a given category in a given input item.
147 * \param p_i The input item to get info from
148 * \param psz_cat String representing the category for the info
149 * \param psz_name String representing the name of the desired info
150 * \return A pointer to the string with the given info if found, or an
151 * empty string otherwise. The caller should free the returned
154 char *input_ItemGetInfo( input_item_t *p_i,
156 const char *psz_name )
160 vlc_mutex_lock( &p_i->lock );
162 for( i = 0 ; i< p_i->i_categories ; i++ )
164 info_category_t *p_cat = p_i->pp_categories[i];
166 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
169 for( j = 0; j < p_cat->i_infos ; j++ )
171 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
173 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
174 vlc_mutex_unlock( &p_i->lock );
179 vlc_mutex_unlock( &p_i->lock );
183 static void input_ItemDestroy ( gc_object_t *p_this )
185 vlc_object_t *p_obj = (vlc_object_t *)p_this->p_destructor_arg;
186 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
187 input_item_t *p_input = (input_item_t *) p_this;
190 input_ItemClean( p_input );
192 vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
194 ARRAY_BSEARCH( priv->input_items,->i_id, int, p_input->i_id, i);
196 ARRAY_REMOVE( priv->input_items, i);
198 vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
203 int input_ItemAddOpt( input_item_t *p_input, const char *psz_option,
206 int err = VLC_SUCCESS;
208 if( psz_option == NULL )
211 vlc_mutex_lock( &p_input->lock );
212 if (flags & VLC_INPUT_OPTION_UNIQUE)
214 for (int i = 0 ; i < p_input->i_options; i++)
215 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
219 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
225 p_input->optflagv = flagv;
226 flagv[p_input->optflagc++] = flags;
228 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
229 p_input->i_options, strdup( psz_option ) );
231 vlc_mutex_unlock( &p_input->lock );
235 int input_ItemAddInfo( input_item_t *p_i,
237 const char *psz_name,
238 const char *psz_format, ... )
242 info_t *p_info = NULL;
243 info_category_t *p_cat = NULL ;
245 vlc_mutex_lock( &p_i->lock );
247 for( i = 0 ; i < p_i->i_categories ; i ++ )
249 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
251 p_cat = p_i->pp_categories[i];
257 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
259 vlc_mutex_unlock( &p_i->lock );
262 p_cat->psz_name = strdup( psz_cat );
265 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
269 for( i = 0; i< p_cat->i_infos; i++ )
271 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
273 p_info = p_cat->pp_infos[i];
280 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
282 vlc_mutex_unlock( &p_i->lock );
285 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
286 p_info->psz_name = strdup( psz_name );
290 free( p_info->psz_value );
293 va_start( args, psz_format );
294 if( vasprintf( &p_info->psz_value, psz_format, args) )
295 p_info->psz_value = NULL;
298 vlc_mutex_unlock( &p_i->lock );
300 return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
303 input_item_t *__input_ItemGetById( vlc_object_t *p_obj, int i_id )
305 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
306 input_item_t * p_ret = NULL;
309 vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
311 ARRAY_BSEARCH( priv->input_items, ->i_id, int, i_id, i);
313 p_ret = ARRAY_VAL( priv->input_items, i);
315 vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
320 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
321 const char *psz_name,
323 const char *const *ppsz_options,
326 return input_ItemNewWithType( p_obj, psz_uri, psz_name,
327 i_options, ppsz_options,
328 i_duration, ITEM_TYPE_UNKNOWN );
332 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
333 const char *psz_name,
335 const char *const *ppsz_options,
339 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
341 DECMALLOC_NULL( p_input, input_item_t );
343 input_ItemInit( p_obj, p_input );
344 vlc_gc_init( p_input, input_ItemDestroy, (void *)p_obj );
346 vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
347 p_input->i_id = ++priv->i_last_input_id;
348 ARRAY_APPEND( priv->input_items, p_input );
349 vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
351 p_input->b_fixed_name = false;
354 p_input->psz_uri = strdup( psz_uri );
356 p_input->psz_uri = NULL;
358 p_input->i_type = i_type;
360 if( p_input->i_type == ITEM_TYPE_UNKNOWN )
361 GuessType( p_input );
363 if( psz_name != NULL )
364 p_input->psz_name = strdup( psz_name );
365 else if( p_input->i_type == ITEM_TYPE_FILE && p_input->psz_uri )
367 const char *psz_filename = strrchr( p_input->psz_uri, DIR_SEP_CHAR );
368 if( psz_filename && *psz_filename == DIR_SEP_CHAR )
370 p_input->psz_name = strdup( psz_filename && *psz_filename
371 ? psz_filename : p_input->psz_uri );
374 p_input->psz_name = p_input->psz_uri ? strdup( p_input->psz_uri ) : NULL;
376 p_input->i_duration = i_duration;
378 for( int i = 0; i < i_options; i++ )
379 input_ItemAddOption( p_input, ppsz_options[i] );
383 /* Guess the type of the item using the beginning of the mrl */
384 static void GuessType( input_item_t *p_item)
387 static struct { const char *psz_search; int i_type; } types_array[] =
389 { "http", ITEM_TYPE_NET },
390 { "dvd", ITEM_TYPE_DISC },
391 { "cdda", ITEM_TYPE_CDDA },
392 { "mms", ITEM_TYPE_NET },
393 { "rtsp", ITEM_TYPE_NET },
394 { "udp", ITEM_TYPE_NET },
395 { "rtp", ITEM_TYPE_NET },
396 { "vcd", ITEM_TYPE_DISC },
397 { "v4l", ITEM_TYPE_CARD },
398 { "dshow", ITEM_TYPE_CARD },
399 { "pvr", ITEM_TYPE_CARD },
400 { "dvb", ITEM_TYPE_CARD },
401 { "qpsk", ITEM_TYPE_CARD },
402 { "sdp", ITEM_TYPE_NET },
406 if( !p_item->psz_uri )
408 p_item->i_type = ITEM_TYPE_FILE;
412 for( i = 0; types_array[i].psz_search != NULL; i++ )
414 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
415 strlen( types_array[i].psz_search ) ) )
417 p_item->i_type = types_array[i].i_type;
421 p_item->i_type = ITEM_TYPE_FILE;