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"
37 static int GuessType( const input_item_t *p_item );
39 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
41 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
43 memset( p_i, 0, sizeof(input_item_t) );
47 TAB_INIT( p_i->i_es, p_i->es );
48 TAB_INIT( p_i->i_options, p_i->ppsz_options );
49 p_i->optflagv = NULL, p_i->optflagc = 0;
50 TAB_INIT( p_i->i_categories, p_i->pp_categories );
51 TAB_INIT( p_i->i_epg, p_i->pp_epg );
53 p_i->i_type = ITEM_TYPE_UNKNOWN;
54 p_i->b_fixed_name = true;
59 vlc_mutex_init( &p_i->lock );
60 vlc_event_manager_t * p_em = &p_i->event_manager;
61 vlc_event_manager_init( p_em, p_i, p_o );
62 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
63 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
64 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
65 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
66 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
67 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
68 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
69 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
72 static inline void input_item_Clean( input_item_t *p_i )
76 vlc_event_manager_fini( &p_i->event_manager );
78 free( p_i->psz_name );
82 vlc_mutex_destroy( &p_i->p_stats->lock );
87 vlc_meta_Delete( p_i->p_meta );
89 for( i = 0; i < p_i->i_options; i++ )
90 free( p_i->ppsz_options[i] );
91 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
94 for( i = 0; i < p_i->i_es; i++ )
96 es_format_Clean( p_i->es[i] );
99 TAB_CLEAN( p_i->i_es, p_i->es );
101 for( i = 0; i < p_i->i_epg; i++ )
102 vlc_epg_Delete( p_i->pp_epg[i] );
103 TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
105 for( i = 0; i < p_i->i_categories; i++ )
106 info_category_Delete( p_i->pp_categories[i] );
107 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
109 vlc_mutex_destroy( &p_i->lock );
111 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
115 vlc_mutex_lock( &p_i->lock );
117 b_changed = p_i->b_error_when_reading != b_error;
118 p_i->b_error_when_reading = b_error;
120 vlc_mutex_unlock( &p_i->lock );
126 event.type = vlc_InputItemErrorWhenReadingChanged;
127 event.u.input_item_error_when_reading_changed.new_value = b_error;
128 vlc_event_send( &p_i->event_manager, &event );
131 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
133 bool b_send_event = false;
135 vlc_mutex_lock( &p_i->lock );
138 p_i->p_meta = vlc_meta_New();
140 int status = vlc_meta_GetStatus(p_i->p_meta);
143 new_status = status | ITEM_PREPARSED;
145 new_status = status & ~ITEM_PREPARSED;
146 if( status != new_status )
148 vlc_meta_SetStatus(p_i->p_meta, new_status);
152 vlc_mutex_unlock( &p_i->lock );
157 event.type = vlc_InputItemPreparsedChanged;
158 event.u.input_item_preparsed_changed.new_status = new_status;
159 vlc_event_send( &p_i->event_manager, &event );
163 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
165 vlc_mutex_lock( &p_i->lock );
168 p_i->p_meta = vlc_meta_New();
170 int status = vlc_meta_GetStatus(p_i->p_meta);
173 status |= ITEM_ART_NOTFOUND;
175 status &= ~ITEM_ART_NOTFOUND;
177 vlc_meta_SetStatus(p_i->p_meta, status);
179 vlc_mutex_unlock( &p_i->lock );
182 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
184 vlc_mutex_lock( &p_i->lock );
187 p_i->p_meta = vlc_meta_New();
189 int status = vlc_meta_GetStatus(p_i->p_meta);
192 status |= ITEM_ART_FETCHED;
194 status &= ~ITEM_ART_FETCHED;
196 vlc_meta_SetStatus(p_i->p_meta, status);
198 vlc_mutex_unlock( &p_i->lock );
201 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
205 vlc_mutex_lock( &p_i->lock );
207 p_i->p_meta = vlc_meta_New();
208 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
209 vlc_mutex_unlock( &p_i->lock );
211 /* Notify interested third parties */
212 event.type = vlc_InputItemMetaChanged;
213 event.u.input_item_meta_changed.meta_type = meta_type;
214 vlc_event_send( &p_i->event_manager, &event );
217 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
218 * consistant with (nearly?) all or copy funcs */
219 void input_item_CopyOptions( input_item_t *p_parent,
220 input_item_t *p_child )
222 vlc_mutex_lock( &p_parent->lock );
224 for( int i = 0 ; i< p_parent->i_options; i++ )
226 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
229 input_item_AddOption( p_child,
230 p_parent->ppsz_options[i],
231 p_parent->optflagv[i] );
234 vlc_mutex_unlock( &p_parent->lock );
237 static void post_subitems( input_item_node_t *p_node )
239 for( int i = 0; i < p_node->i_children; i++ )
242 event.type = vlc_InputItemSubItemAdded;
243 event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
244 vlc_event_send( &p_node->p_item->event_manager, &event );
246 post_subitems( p_node->pp_children[i] );
250 /* This won't hold the item, but can tell to interested third parties
251 * Like the playlist, that there is a new sub item. With this design
252 * It is not the input item's responsability to keep all the ref of
253 * the input item children. */
254 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
256 vlc_mutex_lock( &p_parent->lock );
257 p_parent->i_type = ITEM_TYPE_PLAYLIST;
258 vlc_mutex_unlock( &p_parent->lock );
260 input_item_node_t *p_node = input_item_node_Create( p_parent );
261 input_item_node_AppendItem( p_node, p_child );
262 input_item_node_PostAndDelete( p_node );
265 bool input_item_HasErrorWhenReading( input_item_t *p_item )
267 vlc_mutex_lock( &p_item->lock );
269 bool b_error = p_item->b_error_when_reading;
271 vlc_mutex_unlock( &p_item->lock );
276 bool input_item_MetaMatch( input_item_t *p_i,
277 vlc_meta_type_t meta_type, const char *psz )
279 vlc_mutex_lock( &p_i->lock );
283 vlc_mutex_unlock( &p_i->lock );
286 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
287 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
289 vlc_mutex_unlock( &p_i->lock );
294 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
296 vlc_mutex_lock( &p_i->lock );
300 vlc_mutex_unlock( &p_i->lock );
305 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
306 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
308 vlc_mutex_unlock( &p_i->lock );
312 /* Get the title of a given item or fallback to the name if the title is empty */
313 char *input_item_GetTitleFbName( input_item_t *p_item )
316 vlc_mutex_lock( &p_item->lock );
318 if( !p_item->p_meta )
320 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
321 vlc_mutex_unlock( &p_item->lock );
325 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
326 if( !EMPTY_STR( psz_title ) )
327 psz_ret = strdup( psz_title );
329 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
331 vlc_mutex_unlock( &p_item->lock );
335 char *input_item_GetName( input_item_t *p_item )
337 vlc_mutex_lock( &p_item->lock );
339 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
341 vlc_mutex_unlock( &p_item->lock );
344 void input_item_SetName( input_item_t *p_item, const char *psz_name )
346 vlc_mutex_lock( &p_item->lock );
348 free( p_item->psz_name );
349 p_item->psz_name = strdup( psz_name );
351 vlc_mutex_unlock( &p_item->lock );
354 char *input_item_GetURI( input_item_t *p_i )
356 vlc_mutex_lock( &p_i->lock );
358 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
360 vlc_mutex_unlock( &p_i->lock );
363 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
365 vlc_mutex_lock( &p_i->lock );
367 if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
368 fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
371 free( p_i->psz_uri );
372 p_i->psz_uri = strdup( psz_uri );
374 p_i->i_type = GuessType( p_i );
379 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
381 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
383 if( psz_filename && *psz_filename == '/' )
385 if( psz_filename && *psz_filename )
386 p_i->psz_name = strdup( psz_filename );
388 /* Make the name more readable */
390 decode_URI( p_i->psz_name );
393 { /* Strip login and password from title */
397 vlc_UrlParse( &url, psz_uri, 0 );
398 if( url.psz_protocol )
401 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
402 url.psz_host, url.i_port,
403 url.psz_path ? url.psz_path : "" );
405 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
406 url.psz_host ? url.psz_host : "",
407 url.psz_path ? url.psz_path : "" );
412 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
413 url.psz_path ? url.psz_path : "" );
415 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
416 url.psz_path ? url.psz_path : "" );
418 vlc_UrlClean( &url );
420 p_i->psz_name=NULL; /* recover from undefined value */
423 vlc_mutex_unlock( &p_i->lock );
426 mtime_t input_item_GetDuration( input_item_t *p_i )
428 vlc_mutex_lock( &p_i->lock );
430 mtime_t i_duration = p_i->i_duration;
432 vlc_mutex_unlock( &p_i->lock );
436 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
438 bool b_send_event = false;
440 vlc_mutex_lock( &p_i->lock );
441 if( p_i->i_duration != i_duration )
443 p_i->i_duration = i_duration;
446 vlc_mutex_unlock( &p_i->lock );
452 event.type = vlc_InputItemDurationChanged;
453 event.u.input_item_duration_changed.new_duration = i_duration;
454 vlc_event_send( &p_i->event_manager, &event );
459 bool input_item_IsPreparsed( input_item_t *p_item )
461 vlc_mutex_lock( &p_item->lock );
462 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
463 vlc_mutex_unlock( &p_item->lock );
468 bool input_item_IsArtFetched( input_item_t *p_item )
470 vlc_mutex_lock( &p_item->lock );
471 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
472 vlc_mutex_unlock( &p_item->lock );
477 static void input_item_Destroy ( gc_object_t *p_gc )
479 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
481 input_item_Clean( p_item );
485 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
488 int err = VLC_SUCCESS;
490 if( psz_option == NULL )
493 vlc_mutex_lock( &p_input->lock );
494 if (flags & VLC_INPUT_OPTION_UNIQUE)
496 for (int i = 0 ; i < p_input->i_options; i++)
497 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
501 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
507 p_input->optflagv = flagv;
508 flagv[p_input->optflagc++] = flags;
510 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
511 p_input->i_options, strdup( psz_option ) );
513 vlc_mutex_unlock( &p_input->lock );
517 static info_category_t *InputItemFindCat( input_item_t *p_item,
518 int *pi_index, const char *psz_cat )
520 vlc_assert_locked( &p_item->lock );
521 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
523 info_category_t *p_cat = p_item->pp_categories[i];
525 if( !strcmp( p_cat->psz_name, psz_cat ) )
536 * Get a info item from a given category in a given input item.
538 * \param p_i The input item to get info from
539 * \param psz_cat String representing the category for the info
540 * \param psz_name String representing the name of the desired info
541 * \return A pointer to the string with the given info if found, or an
542 * empty string otherwise. The caller should free the returned
545 char *input_item_GetInfo( input_item_t *p_i,
547 const char *psz_name )
549 vlc_mutex_lock( &p_i->lock );
551 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
554 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
555 if( p_info && p_info->psz_value )
557 char *psz_ret = strdup( p_info->psz_value );
558 vlc_mutex_unlock( &p_i->lock );
562 vlc_mutex_unlock( &p_i->lock );
566 static int InputItemVaAddInfo( input_item_t *p_i,
568 const char *psz_name,
569 const char *psz_format, va_list args )
571 vlc_assert_locked( &p_i->lock );
573 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
576 p_cat = info_category_New( psz_cat );
579 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
582 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
583 if( !p_info || !p_info->psz_value )
588 static int InputItemAddInfo( input_item_t *p_i,
590 const char *psz_name,
591 const char *psz_format, ... )
595 va_start( args, psz_format );
596 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
602 int input_item_AddInfo( input_item_t *p_i,
604 const char *psz_name,
605 const char *psz_format, ... )
609 vlc_mutex_lock( &p_i->lock );
611 va_start( args, psz_format );
612 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
615 vlc_mutex_unlock( &p_i->lock );
622 event.type = vlc_InputItemInfoChanged;
623 vlc_event_send( &p_i->event_manager, &event );
628 int input_item_DelInfo( input_item_t *p_i,
630 const char *psz_name )
632 vlc_mutex_lock( &p_i->lock );
634 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
637 vlc_mutex_unlock( &p_i->lock );
643 /* Remove a specific info */
644 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
647 vlc_mutex_unlock( &p_i->lock );
653 /* Remove the complete categorie */
654 info_category_Delete( p_cat );
655 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
657 vlc_mutex_unlock( &p_i->lock );
661 event.type = vlc_InputItemInfoChanged;
662 vlc_event_send( &p_i->event_manager, &event );
666 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
668 vlc_mutex_lock( &p_item->lock );
670 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
673 info_category_Delete( p_old );
674 p_item->pp_categories[i_cat] = p_cat;
678 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
681 vlc_mutex_unlock( &p_item->lock );
685 event.type = vlc_InputItemInfoChanged;
686 vlc_event_send( &p_item->event_manager, &event );
688 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
690 vlc_mutex_lock( &p_item->lock );
691 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
694 for( int i = 0; i < p_cat->i_infos; i++ )
695 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
696 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
697 info_category_Delete( p_cat );
701 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
704 vlc_mutex_unlock( &p_item->lock );
708 event.type = vlc_InputItemInfoChanged;
709 vlc_event_send( &p_item->event_manager, &event );
713 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
715 vlc_mutex_lock( &p_item->lock );
718 vlc_epg_t *p_epg = NULL;
719 for( int i = 0; i < p_item->i_epg; i++ )
721 vlc_epg_t *p_tmp = p_item->pp_epg[i];
723 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
725 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
735 p_epg = vlc_epg_New( p_update->psz_name );
737 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
740 vlc_epg_Merge( p_epg, p_update );
742 vlc_mutex_unlock( &p_item->lock );
746 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
749 input_item_DelInfo( p_item, psz_epg, NULL );
751 vlc_mutex_lock( &p_item->lock );
752 for( int i = 0; i < p_epg->i_event; i++ )
754 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
755 time_t t_start = (time_t)p_evt->i_start;
759 localtime_r( &t_start, &tm_start );
761 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
762 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
763 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
764 if( p_evt->psz_short_description || p_evt->psz_description )
765 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
767 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
768 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
769 p_evt->psz_description ? p_evt->psz_description : "" );
771 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
773 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
775 vlc_mutex_unlock( &p_item->lock );
780 if( p_epg->i_event > 0 )
782 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
783 vlc_event_send( &p_item->event_manager, &event );
787 void input_item_SetEpgOffline( input_item_t *p_item )
789 vlc_mutex_lock( &p_item->lock );
790 for( int i = 0; i < p_item->i_epg; i++ )
791 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
792 vlc_mutex_unlock( &p_item->lock );
795 vlc_mutex_lock( &p_item->lock );
796 const int i_epg_info = p_item->i_epg;
797 char *ppsz_epg_info[i_epg_info];
798 for( int i = 0; i < p_item->i_epg; i++ )
800 const vlc_epg_t *p_epg = p_item->pp_epg[i];
801 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
802 ppsz_epg_info[i] = NULL;
804 vlc_mutex_unlock( &p_item->lock );
806 for( int i = 0; i < i_epg_info; i++ )
808 if( !ppsz_epg_info[i] )
810 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
811 free( ppsz_epg_info[i] );
815 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
816 vlc_event_send( &p_item->event_manager, &event );
819 #undef input_item_NewExt
820 input_item_t *input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
821 const char *psz_name,
823 const char *const *ppsz_options,
824 unsigned i_option_flags,
827 return input_item_NewWithType( p_obj, psz_uri, psz_name,
828 i_options, ppsz_options, i_option_flags,
829 i_duration, ITEM_TYPE_UNKNOWN );
833 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
834 const char *psz_name,
836 const char *const *ppsz_options,
837 unsigned i_option_flags,
841 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
842 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
844 input_item_t* p_input = malloc( sizeof(input_item_t ) );
848 input_item_Init( p_obj, p_input );
849 vlc_gc_init( p_input, input_item_Destroy );
851 vlc_mutex_lock( &input_id_lock );
852 p_input->i_id = ++priv->i_last_input_id;
853 vlc_mutex_unlock( &input_id_lock );
855 p_input->b_fixed_name = false;
857 p_input->i_type = i_type;
858 p_input->b_prefers_tree = false;
861 input_item_SetURI( p_input, psz_uri );
863 if( i_type != ITEM_TYPE_UNKNOWN )
864 p_input->i_type = i_type;
867 input_item_SetName( p_input, psz_name );
869 p_input->i_duration = i_duration;
871 for( int i = 0; i < i_options; i++ )
872 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
876 struct item_type_entry
878 const char psz_scheme[7];
882 static int typecmp( const void *key, const void *entry )
884 const struct item_type_entry *type = entry;
885 const char *uri = key, *scheme = type->psz_scheme;
887 return strncmp( uri, scheme, strlen( scheme ) );
890 /* Guess the type of the item using the beginning of the mrl */
891 static int GuessType( const input_item_t *p_item )
893 static const struct item_type_entry tab[] =
894 { /* /!\ Alphabetical order /!\ */
895 /* Short match work, not just exact match */
896 { "alsa", ITEM_TYPE_CARD },
897 { "atsc", ITEM_TYPE_CARD },
898 { "bd", ITEM_TYPE_DISC },
899 { "cable", ITEM_TYPE_CARD },
900 { "cdda", ITEM_TYPE_CDDA },
901 { "dc1394", ITEM_TYPE_CARD },
902 { "dccp", ITEM_TYPE_NET },
903 { "dir", ITEM_TYPE_DIRECTORY },
904 { "dshow", ITEM_TYPE_CARD },
905 { "dv", ITEM_TYPE_CARD },
906 { "dvb", ITEM_TYPE_CARD },
907 { "dvd", ITEM_TYPE_DISC },
908 { "ftp", ITEM_TYPE_NET },
909 { "http", ITEM_TYPE_NET },
910 { "icyx", ITEM_TYPE_NET },
911 { "itpc", ITEM_TYPE_NET },
912 { "jack", ITEM_TYPE_CARD },
913 { "live", ITEM_TYPE_NET }, /* livedotcom */
914 { "mms", ITEM_TYPE_NET },
915 { "mtp", ITEM_TYPE_DISC },
916 { "ofdm", ITEM_TYPE_CARD },
917 { "oss", ITEM_TYPE_CARD },
918 { "pnm", ITEM_TYPE_NET },
919 { "pvr", ITEM_TYPE_CARD },
920 { "qam", ITEM_TYPE_CARD },
921 { "qpsk", ITEM_TYPE_CARD },
922 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
923 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
924 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
925 { "satell", ITEM_TYPE_CARD }, /* sattelite */
926 { "screen", ITEM_TYPE_CARD },
927 { "sdp", ITEM_TYPE_NET },
928 { "smb", ITEM_TYPE_NET },
929 { "svcd", ITEM_TYPE_DISC },
930 { "tcp", ITEM_TYPE_NET },
931 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
932 { "udp", ITEM_TYPE_NET }, /* udplite too */
933 { "unsv", ITEM_TYPE_NET },
934 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
935 { "v4l", ITEM_TYPE_CARD },
936 { "vcd", ITEM_TYPE_DISC },
937 { "window", ITEM_TYPE_CARD },
939 const struct item_type_entry *e;
941 if( !strstr( p_item->psz_uri, "://" ) )
942 return ITEM_TYPE_FILE;
944 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
945 sizeof( tab[0] ), typecmp );
946 return e ? e->i_type : ITEM_TYPE_FILE;
949 input_item_node_t *input_item_node_Create( input_item_t *p_input )
951 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
957 p_node->p_item = p_input;
958 vlc_gc_incref( p_input );
960 p_node->p_parent = NULL;
961 p_node->i_children = 0;
962 p_node->pp_children = NULL;
967 static void RecursiveNodeDelete( input_item_node_t *p_node )
969 for( int i = 0; i < p_node->i_children; i++ )
970 RecursiveNodeDelete( p_node->pp_children[i] );
972 vlc_gc_decref( p_node->p_item );
973 free( p_node->pp_children );
977 void input_item_node_Delete( input_item_node_t *p_node )
979 if( p_node->p_parent )
981 for( int i = 0; i < p_node->p_parent->i_children; i++ )
982 if( p_node->p_parent->pp_children[i] == p_node )
984 REMOVE_ELEM( p_node->p_parent->pp_children,
985 p_node->p_parent->i_children,
991 RecursiveNodeDelete( p_node );
994 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
996 input_item_node_t *p_new_child = input_item_node_Create( p_item );
997 if( !p_new_child ) return NULL;
998 input_item_node_AppendNode( p_node, p_new_child );
1002 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1004 assert( p_parent && p_child && p_child->p_parent == NULL );
1005 INSERT_ELEM( p_parent->pp_children,
1006 p_parent->i_children,
1007 p_parent->i_children,
1009 p_child->p_parent = p_parent;
1012 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1014 post_subitems( p_root );
1017 event.type = vlc_InputItemSubItemTreeAdded;
1018 event.u.input_item_subitem_tree_added.p_root = p_root;
1019 vlc_event_send( &p_root->p_item->event_manager, &event );
1021 input_item_node_Delete( p_root );