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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
36 static int GuessType( const input_item_t *p_item );
38 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
40 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
42 memset( p_i, 0, sizeof(input_item_t) );
46 TAB_INIT( p_i->i_es, p_i->es );
47 TAB_INIT( p_i->i_options, p_i->ppsz_options );
48 p_i->optflagv = NULL, p_i->optflagc = 0;
49 TAB_INIT( p_i->i_categories, p_i->pp_categories );
50 TAB_INIT( p_i->i_epg, p_i->pp_epg );
52 p_i->i_type = ITEM_TYPE_UNKNOWN;
53 p_i->b_fixed_name = true;
58 vlc_mutex_init( &p_i->lock );
59 vlc_event_manager_t * p_em = &p_i->event_manager;
60 vlc_event_manager_init( p_em, p_i, p_o );
61 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
62 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
63 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
64 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
65 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
66 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
67 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
68 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
71 static inline void input_item_Clean( input_item_t *p_i )
75 vlc_event_manager_fini( &p_i->event_manager );
77 free( p_i->psz_name );
81 vlc_mutex_destroy( &p_i->p_stats->lock );
86 vlc_meta_Delete( p_i->p_meta );
88 for( i = 0; i < p_i->i_options; i++ )
89 free( p_i->ppsz_options[i] );
90 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
93 for( i = 0; i < p_i->i_es; i++ )
95 es_format_Clean( p_i->es[i] );
98 TAB_CLEAN( p_i->i_es, p_i->es );
100 for( i = 0; i < p_i->i_epg; i++ )
101 vlc_epg_Delete( p_i->pp_epg[i] );
102 TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
104 for( i = 0; i < p_i->i_categories; i++ )
106 info_category_t *p_category = p_i->pp_categories[i];
109 for( j = 0; j < p_category->i_infos; j++ )
111 struct info_t *p_info = p_category->pp_infos[j];
113 free( p_info->psz_name);
114 free( p_info->psz_value );
117 TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
119 free( p_category->psz_name );
122 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
124 vlc_mutex_destroy( &p_i->lock );
126 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
130 vlc_mutex_lock( &p_i->lock );
132 b_changed = p_i->b_error_when_reading != b_error;
133 p_i->b_error_when_reading = b_error;
135 vlc_mutex_unlock( &p_i->lock );
141 event.type = vlc_InputItemErrorWhenReadingChanged;
142 event.u.input_item_error_when_reading_changed.new_value = b_error;
143 vlc_event_send( &p_i->event_manager, &event );
146 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
148 bool b_send_event = false;
150 vlc_mutex_lock( &p_i->lock );
153 p_i->p_meta = vlc_meta_New();
155 int status = vlc_meta_GetStatus(p_i->p_meta);
158 new_status = status | ITEM_PREPARSED;
160 new_status = status & ~ITEM_PREPARSED;
161 if( status != new_status )
163 vlc_meta_SetStatus(p_i->p_meta, new_status);
167 vlc_mutex_unlock( &p_i->lock );
172 event.type = vlc_InputItemPreparsedChanged;
173 event.u.input_item_preparsed_changed.new_status = new_status;
174 vlc_event_send( &p_i->event_manager, &event );
178 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
180 vlc_mutex_lock( &p_i->lock );
183 p_i->p_meta = vlc_meta_New();
185 int status = vlc_meta_GetStatus(p_i->p_meta);
188 status |= ITEM_ART_NOTFOUND;
190 status &= ~ITEM_ART_NOTFOUND;
192 vlc_meta_SetStatus(p_i->p_meta, status);
194 vlc_mutex_unlock( &p_i->lock );
197 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
199 vlc_mutex_lock( &p_i->lock );
202 p_i->p_meta = vlc_meta_New();
204 int status = vlc_meta_GetStatus(p_i->p_meta);
207 status |= ITEM_ART_FETCHED;
209 status &= ~ITEM_ART_FETCHED;
211 vlc_meta_SetStatus(p_i->p_meta, status);
213 vlc_mutex_unlock( &p_i->lock );
216 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
220 vlc_mutex_lock( &p_i->lock );
222 p_i->p_meta = vlc_meta_New();
223 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
224 vlc_mutex_unlock( &p_i->lock );
226 /* Notify interested third parties */
227 event.type = vlc_InputItemMetaChanged;
228 event.u.input_item_meta_changed.meta_type = meta_type;
229 vlc_event_send( &p_i->event_manager, &event );
232 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
233 * consistant with (nearly?) all or copy funcs */
234 void input_item_CopyOptions( input_item_t *p_parent,
235 input_item_t *p_child )
237 vlc_mutex_lock( &p_parent->lock );
239 for( int i = 0 ; i< p_parent->i_options; i++ )
241 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
244 input_item_AddOption( p_child,
245 p_parent->ppsz_options[i],
246 p_parent->optflagv[i] );
249 vlc_mutex_unlock( &p_parent->lock );
252 static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
254 /* Notify interested third parties */
256 event.type = vlc_InputItemSubItemAdded;
257 event.u.input_item_subitem_added.p_new_child = p_child;
258 vlc_event_send( &p_parent->event_manager, &event );
261 /* This won't hold the item, but can tell to interested third parties
262 * Like the playlist, that there is a new sub item. With this design
263 * It is not the input item's responsability to keep all the ref of
264 * the input item children. */
265 void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
267 vlc_mutex_lock( &p_parent->lock );
268 p_parent->i_type = ITEM_TYPE_PLAYLIST;
269 vlc_mutex_unlock( &p_parent->lock );
271 notify_subitem_added(p_parent, p_child);
273 input_item_node_t *p_node = input_item_node_Create( p_parent );
274 input_item_node_AppendItem( p_node, p_child );
275 input_item_AddSubItemTree( p_node );
276 input_item_node_Delete( p_node );
279 void input_item_AddSubItemTree ( input_item_node_t *p_root )
282 event.type = vlc_InputItemSubItemTreeAdded;
283 event.u.input_item_subitem_tree_added.p_root = p_root;
284 vlc_event_send( &p_root->p_item->event_manager, &event );
287 bool input_item_HasErrorWhenReading( input_item_t *p_item )
289 vlc_mutex_lock( &p_item->lock );
291 bool b_error = p_item->b_error_when_reading;
293 vlc_mutex_unlock( &p_item->lock );
298 bool input_item_MetaMatch( input_item_t *p_i,
299 vlc_meta_type_t meta_type, const char *psz )
301 vlc_mutex_lock( &p_i->lock );
305 vlc_mutex_unlock( &p_i->lock );
308 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
309 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
311 vlc_mutex_unlock( &p_i->lock );
316 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
318 vlc_mutex_lock( &p_i->lock );
322 vlc_mutex_unlock( &p_i->lock );
327 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
328 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
330 vlc_mutex_unlock( &p_i->lock );
334 /* Get the title of a given item or fallback to the name if the title is empty */
335 char *input_item_GetTitleFbName( input_item_t *p_item )
338 vlc_mutex_lock( &p_item->lock );
340 if( !p_item->p_meta )
342 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
343 vlc_mutex_unlock( &p_item->lock );
347 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
348 if( !EMPTY_STR( psz_title ) )
349 psz_ret = strdup( psz_title );
351 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
353 vlc_mutex_unlock( &p_item->lock );
357 char *input_item_GetName( input_item_t *p_item )
359 vlc_mutex_lock( &p_item->lock );
361 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
363 vlc_mutex_unlock( &p_item->lock );
366 void input_item_SetName( input_item_t *p_item, const char *psz_name )
368 vlc_mutex_lock( &p_item->lock );
370 free( p_item->psz_name );
371 p_item->psz_name = strdup( psz_name );
373 vlc_mutex_unlock( &p_item->lock );
376 char *input_item_GetURI( input_item_t *p_i )
378 vlc_mutex_lock( &p_i->lock );
380 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
382 vlc_mutex_unlock( &p_i->lock );
385 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
387 vlc_mutex_lock( &p_i->lock );
389 if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
390 fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
393 free( p_i->psz_uri );
394 p_i->psz_uri = strdup( psz_uri );
396 p_i->i_type = GuessType( p_i );
401 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
403 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
405 if( psz_filename && *psz_filename == '/' )
407 if( psz_filename && *psz_filename )
408 p_i->psz_name = strdup( psz_filename );
410 /* Make the name more readable */
412 decode_URI( p_i->psz_name );
415 { /* Strip login and password from title */
419 vlc_UrlParse( &url, psz_uri, 0 );
420 if( url.psz_protocol )
423 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
424 url.psz_host, url.i_port,
425 url.psz_path ? url.psz_path : "" );
427 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
428 url.psz_host ? url.psz_host : "",
429 url.psz_path ? url.psz_path : "" );
434 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
435 url.psz_path ? url.psz_path : "" );
437 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
438 url.psz_path ? url.psz_path : "" );
440 vlc_UrlClean( &url );
442 p_i->psz_name=NULL; /* recover from undefined value */
445 vlc_mutex_unlock( &p_i->lock );
448 mtime_t input_item_GetDuration( input_item_t *p_i )
450 vlc_mutex_lock( &p_i->lock );
452 mtime_t i_duration = p_i->i_duration;
454 vlc_mutex_unlock( &p_i->lock );
458 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
460 bool b_send_event = false;
462 vlc_mutex_lock( &p_i->lock );
463 if( p_i->i_duration != i_duration )
465 p_i->i_duration = i_duration;
468 vlc_mutex_unlock( &p_i->lock );
474 event.type = vlc_InputItemDurationChanged;
475 event.u.input_item_duration_changed.new_duration = i_duration;
476 vlc_event_send( &p_i->event_manager, &event );
481 bool input_item_IsPreparsed( input_item_t *p_item )
483 vlc_mutex_lock( &p_item->lock );
484 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
485 vlc_mutex_unlock( &p_item->lock );
490 bool input_item_IsArtFetched( input_item_t *p_item )
492 vlc_mutex_lock( &p_item->lock );
493 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
494 vlc_mutex_unlock( &p_item->lock );
499 static void input_item_Destroy ( gc_object_t *p_gc )
501 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
503 input_item_Clean( p_item );
507 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
510 int err = VLC_SUCCESS;
512 if( psz_option == NULL )
515 vlc_mutex_lock( &p_input->lock );
516 if (flags & VLC_INPUT_OPTION_UNIQUE)
518 for (int i = 0 ; i < p_input->i_options; i++)
519 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
523 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
529 p_input->optflagv = flagv;
530 flagv[p_input->optflagc++] = flags;
532 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
533 p_input->i_options, strdup( psz_option ) );
535 vlc_mutex_unlock( &p_input->lock );
540 * Get a info item from a given category in a given input item.
542 * \param p_i The input item to get info from
543 * \param psz_cat String representing the category for the info
544 * \param psz_name String representing the name of the desired info
545 * \return A pointer to the string with the given info if found, or an
546 * empty string otherwise. The caller should free the returned
549 char *input_item_GetInfo( input_item_t *p_i,
551 const char *psz_name )
553 vlc_mutex_lock( &p_i->lock );
555 for( int i = 0; i< p_i->i_categories; i++ )
557 const info_category_t *p_cat = p_i->pp_categories[i];
559 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
562 for( int j = 0; j < p_cat->i_infos; j++ )
564 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
566 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
567 vlc_mutex_unlock( &p_i->lock );
572 vlc_mutex_unlock( &p_i->lock );
576 static int InputItemVaAddInfo( input_item_t *p_i,
578 const char *psz_name,
579 const char *psz_format, va_list args )
582 info_t *p_info = NULL;
583 info_category_t *p_cat = NULL ;
585 vlc_assert_locked( &p_i->lock );
587 for( i = 0 ; i < p_i->i_categories ; i ++ )
589 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
591 p_cat = p_i->pp_categories[i];
597 if( !(p_cat = malloc( sizeof(*p_cat) )) )
600 p_cat->psz_name = strdup( psz_cat );
603 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
607 for( i = 0; i< p_cat->i_infos; i++ )
609 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
611 p_info = p_cat->pp_infos[i];
618 if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
621 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
622 p_info->psz_name = strdup( psz_name );
626 free( p_info->psz_value );
629 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
630 p_info->psz_value = NULL;
632 return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
635 static int InputItemAddInfo( input_item_t *p_i,
637 const char *psz_name,
638 const char *psz_format, ... )
642 va_start( args, psz_format );
643 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
649 int input_item_AddInfo( input_item_t *p_i,
651 const char *psz_name,
652 const char *psz_format, ... )
656 vlc_mutex_lock( &p_i->lock );
658 va_start( args, psz_format );
659 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
662 vlc_mutex_unlock( &p_i->lock );
669 event.type = vlc_InputItemInfoChanged;
670 vlc_event_send( &p_i->event_manager, &event );
675 int input_item_DelInfo( input_item_t *p_i,
677 const char *psz_name )
679 info_category_t *p_cat = NULL;
683 vlc_mutex_lock( &p_i->lock );
684 for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
686 if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
689 p_cat = p_i->pp_categories[i_cat];
695 vlc_mutex_unlock( &p_i->lock );
701 /* Remove a specific info */
702 for( i = 0; i < p_cat->i_infos; i++ )
704 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
706 free( p_cat->pp_infos[i]->psz_name );
707 free( p_cat->pp_infos[i]->psz_value );
708 free( p_cat->pp_infos[i] );
709 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
713 if( i >= p_cat->i_infos )
715 vlc_mutex_unlock( &p_i->lock );
721 /* Remove the complete categorie */
722 for( i = 0; i < p_cat->i_infos; i++ )
724 free( p_cat->pp_infos[i]->psz_name );
725 free( p_cat->pp_infos[i]->psz_value );
726 free( p_cat->pp_infos[i] );
728 free( p_cat->pp_infos );
729 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
733 free( p_cat->psz_name );
735 vlc_mutex_unlock( &p_i->lock );
739 event.type = vlc_InputItemInfoChanged;
740 vlc_event_send( &p_i->event_manager, &event );
746 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
748 vlc_mutex_lock( &p_item->lock );
751 vlc_epg_t *p_epg = NULL;
752 for( int i = 0; i < p_item->i_epg; i++ )
754 vlc_epg_t *p_tmp = p_item->pp_epg[i];
756 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
758 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
768 p_epg = vlc_epg_New( p_update->psz_name );
770 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
773 vlc_epg_Merge( p_epg, p_update );
775 vlc_mutex_unlock( &p_item->lock );
779 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
782 input_item_DelInfo( p_item, psz_epg, NULL );
784 vlc_mutex_lock( &p_item->lock );
785 for( int i = 0; i < p_epg->i_event; i++ )
787 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
788 time_t t_start = (time_t)p_evt->i_start;
792 localtime_r( &t_start, &tm_start );
794 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
795 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
796 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
797 if( p_evt->psz_short_description || p_evt->psz_description )
798 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
800 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
801 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
802 p_evt->psz_description ? p_evt->psz_description : "" );
804 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
806 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
808 vlc_mutex_unlock( &p_item->lock );
813 if( p_epg->i_event > 0 )
815 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
816 vlc_event_send( &p_item->event_manager, &event );
820 void input_item_SetEpgOffline( input_item_t *p_item )
822 vlc_mutex_lock( &p_item->lock );
823 for( int i = 0; i < p_item->i_epg; i++ )
824 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
825 vlc_mutex_unlock( &p_item->lock );
828 vlc_mutex_lock( &p_item->lock );
829 const int i_epg_info = p_item->i_epg;
830 char *ppsz_epg_info[i_epg_info];
831 for( int i = 0; i < p_item->i_epg; i++ )
833 const vlc_epg_t *p_epg = p_item->pp_epg[i];
834 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
835 ppsz_epg_info[i] = NULL;
837 vlc_mutex_unlock( &p_item->lock );
839 for( int i = 0; i < i_epg_info; i++ )
841 if( !ppsz_epg_info[i] )
843 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
844 free( ppsz_epg_info[i] );
848 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
849 vlc_event_send( &p_item->event_manager, &event );
853 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
854 const char *psz_name,
856 const char *const *ppsz_options,
857 unsigned i_option_flags,
860 return input_item_NewWithType( p_obj, psz_uri, psz_name,
861 i_options, ppsz_options, i_option_flags,
862 i_duration, ITEM_TYPE_UNKNOWN );
866 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
867 const char *psz_name,
869 const char *const *ppsz_options,
870 unsigned i_option_flags,
874 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
875 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
877 input_item_t* p_input = malloc( sizeof(input_item_t ) );
881 input_item_Init( p_obj, p_input );
882 vlc_gc_init( p_input, input_item_Destroy );
884 vlc_mutex_lock( &input_id_lock );
885 p_input->i_id = ++priv->i_last_input_id;
886 vlc_mutex_unlock( &input_id_lock );
888 p_input->b_fixed_name = false;
890 p_input->i_type = i_type;
891 p_input->b_prefers_tree = false;
894 input_item_SetURI( p_input, psz_uri );
896 if( i_type != ITEM_TYPE_UNKNOWN )
897 p_input->i_type = i_type;
900 input_item_SetName( p_input, psz_name );
902 p_input->i_duration = i_duration;
904 for( int i = 0; i < i_options; i++ )
905 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
909 struct item_type_entry
911 const char psz_scheme[7];
915 static int typecmp( const void *key, const void *entry )
917 const struct item_type_entry *type = entry;
918 const char *uri = key, *scheme = type->psz_scheme;
920 return strncmp( uri, scheme, strlen( scheme ) );
923 /* Guess the type of the item using the beginning of the mrl */
924 static int GuessType( const input_item_t *p_item )
926 static const struct item_type_entry tab[] =
927 { /* /!\ Alphabetical order /!\ */
928 /* Short match work, not just exact match */
929 { "alsa", ITEM_TYPE_CARD },
930 { "atsc", ITEM_TYPE_CARD },
931 { "bd", ITEM_TYPE_DISC },
932 { "cable", ITEM_TYPE_CARD },
933 { "cdda", ITEM_TYPE_CDDA },
934 { "dc1394", ITEM_TYPE_CARD },
935 { "dccp", ITEM_TYPE_NET },
936 { "dir", ITEM_TYPE_DIRECTORY },
937 { "dshow", ITEM_TYPE_CARD },
938 { "dv", ITEM_TYPE_CARD },
939 { "dvb", ITEM_TYPE_CARD },
940 { "dvd", ITEM_TYPE_DISC },
941 { "ftp", ITEM_TYPE_NET },
942 { "http", ITEM_TYPE_NET },
943 { "icyx", ITEM_TYPE_NET },
944 { "itpc", ITEM_TYPE_NET },
945 { "jack", ITEM_TYPE_CARD },
946 { "live", ITEM_TYPE_NET }, /* livedotcom */
947 { "mms", ITEM_TYPE_NET },
948 { "mtp", ITEM_TYPE_DISC },
949 { "ofdm", ITEM_TYPE_CARD },
950 { "oss", ITEM_TYPE_CARD },
951 { "pnm", ITEM_TYPE_NET },
952 { "pvr", ITEM_TYPE_CARD },
953 { "qam", ITEM_TYPE_CARD },
954 { "qpsk", ITEM_TYPE_CARD },
955 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
956 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
957 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
958 { "satell", ITEM_TYPE_CARD }, /* sattelite */
959 { "screen", ITEM_TYPE_CARD },
960 { "sdp", ITEM_TYPE_NET },
961 { "smb", ITEM_TYPE_NET },
962 { "svcd", ITEM_TYPE_DISC },
963 { "tcp", ITEM_TYPE_NET },
964 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
965 { "udp", ITEM_TYPE_NET }, /* udplite too */
966 { "unsv", ITEM_TYPE_NET },
967 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
968 { "v4l", ITEM_TYPE_CARD },
969 { "vcd", ITEM_TYPE_DISC },
970 { "window", ITEM_TYPE_CARD },
972 const struct item_type_entry *e;
974 if( !strstr( p_item->psz_uri, "://" ) )
975 return ITEM_TYPE_FILE;
977 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
978 sizeof( tab[0] ), typecmp );
979 return e ? e->i_type : ITEM_TYPE_FILE;
982 input_item_node_t *input_item_node_Create( input_item_t *p_input )
984 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
990 p_node->p_item = p_input;
991 vlc_gc_incref( p_input );
993 p_node->p_parent = NULL;
994 p_node->i_children = 0;
995 p_node->pp_children = NULL;
1000 static void RecursiveNodeDelete( input_item_node_t *p_node )
1002 for( int i = 0; i < p_node->i_children; i++ )
1003 RecursiveNodeDelete( p_node->pp_children[i] );
1005 vlc_gc_decref( p_node->p_item );
1006 free( p_node->pp_children );
1010 void input_item_node_Delete( input_item_node_t *p_node )
1012 if( p_node->p_parent )
1014 for( int i = 0; i < p_node->p_parent->i_children; i++ )
1015 if( p_node->p_parent->pp_children[i] == p_node )
1017 REMOVE_ELEM( p_node->p_parent->pp_children,
1018 p_node->p_parent->i_children,
1024 RecursiveNodeDelete( p_node );
1027 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1029 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1030 if( !p_new_child ) return NULL;
1031 input_item_node_AppendNode( p_node, p_new_child );
1035 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1037 notify_subitem_added(p_parent->p_item, p_child->p_item);
1039 assert( p_parent && p_child && p_child->p_parent == NULL );
1040 INSERT_ELEM( p_parent->pp_children,
1041 p_parent->i_children,
1042 p_parent->i_children,
1044 p_child->p_parent = p_parent;