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 /* This won't hold the item, but can tell to interested third parties
253 * Like the playlist, that there is a new sub item. With this design
254 * It is not the input item's responsability to keep all the ref of
255 * the input item children. */
256 void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
258 vlc_mutex_lock( &p_parent->lock );
260 p_parent->i_type = ITEM_TYPE_PLAYLIST;
262 vlc_mutex_unlock( &p_parent->lock );
264 /* Notify interested third parties */
267 event.type = vlc_InputItemSubItemAdded;
268 event.u.input_item_subitem_added.p_new_child = p_child;
269 vlc_event_send( &p_parent->event_manager, &event );
272 void input_item_AddSubItemTree ( input_item_node_t *p_root )
275 event.type = vlc_InputItemSubItemTreeAdded;
276 event.u.input_item_subitem_tree_added.p_root = p_root;
277 vlc_event_send( &p_root->p_item->event_manager, &event );
280 void input_item_AddSubItem2 ( input_item_t *p_parent, input_item_t *p_child )
282 input_item_node_t *p_node = input_item_node_Create( p_parent );
283 input_item_node_AppendItem( p_node, p_child );
284 input_item_AddSubItemTree( p_node );
285 input_item_node_Delete( p_node );
288 bool input_item_HasErrorWhenReading( input_item_t *p_item )
290 vlc_mutex_lock( &p_item->lock );
292 bool b_error = p_item->b_error_when_reading;
294 vlc_mutex_unlock( &p_item->lock );
299 bool input_item_MetaMatch( input_item_t *p_i,
300 vlc_meta_type_t meta_type, const char *psz )
302 vlc_mutex_lock( &p_i->lock );
306 vlc_mutex_unlock( &p_i->lock );
309 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
310 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
312 vlc_mutex_unlock( &p_i->lock );
317 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
319 vlc_mutex_lock( &p_i->lock );
323 vlc_mutex_unlock( &p_i->lock );
328 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
329 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
331 vlc_mutex_unlock( &p_i->lock );
335 /* Get the title of a given item or fallback to the name if the title is empty */
336 char *input_item_GetTitleFbName( input_item_t *p_item )
339 vlc_mutex_lock( &p_item->lock );
341 if( !p_item->p_meta )
343 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
344 vlc_mutex_unlock( &p_item->lock );
348 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
349 if( !EMPTY_STR( psz_title ) )
350 psz_ret = strdup( psz_title );
352 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
354 vlc_mutex_unlock( &p_item->lock );
358 char *input_item_GetName( input_item_t *p_item )
360 vlc_mutex_lock( &p_item->lock );
362 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
364 vlc_mutex_unlock( &p_item->lock );
367 void input_item_SetName( input_item_t *p_item, const char *psz_name )
369 vlc_mutex_lock( &p_item->lock );
371 free( p_item->psz_name );
372 p_item->psz_name = strdup( psz_name );
374 vlc_mutex_unlock( &p_item->lock );
377 char *input_item_GetURI( input_item_t *p_i )
379 vlc_mutex_lock( &p_i->lock );
381 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
383 vlc_mutex_unlock( &p_i->lock );
386 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
388 vlc_mutex_lock( &p_i->lock );
390 if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
391 fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
394 free( p_i->psz_uri );
395 p_i->psz_uri = strdup( psz_uri );
397 p_i->i_type = GuessType( p_i );
402 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
404 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
406 if( psz_filename && *psz_filename == '/' )
408 if( psz_filename && *psz_filename )
409 p_i->psz_name = strdup( psz_filename );
411 /* Make the name more readable */
413 decode_URI( p_i->psz_name );
416 { /* Strip login and password from title */
420 vlc_UrlParse( &url, psz_uri, 0 );
421 if( url.psz_protocol )
424 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
425 url.psz_host, url.i_port,
426 url.psz_path ? url.psz_path : "" );
428 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
429 url.psz_host ? url.psz_host : "",
430 url.psz_path ? url.psz_path : "" );
435 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
436 url.psz_path ? url.psz_path : "" );
438 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
439 url.psz_path ? url.psz_path : "" );
441 vlc_UrlClean( &url );
443 p_i->psz_name=NULL; /* recover from undefined value */
446 vlc_mutex_unlock( &p_i->lock );
449 mtime_t input_item_GetDuration( input_item_t *p_i )
451 vlc_mutex_lock( &p_i->lock );
453 mtime_t i_duration = p_i->i_duration;
455 vlc_mutex_unlock( &p_i->lock );
459 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
461 bool b_send_event = false;
463 vlc_mutex_lock( &p_i->lock );
464 if( p_i->i_duration != i_duration )
466 p_i->i_duration = i_duration;
469 vlc_mutex_unlock( &p_i->lock );
475 event.type = vlc_InputItemDurationChanged;
476 event.u.input_item_duration_changed.new_duration = i_duration;
477 vlc_event_send( &p_i->event_manager, &event );
482 bool input_item_IsPreparsed( input_item_t *p_item )
484 vlc_mutex_lock( &p_item->lock );
485 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
486 vlc_mutex_unlock( &p_item->lock );
491 bool input_item_IsArtFetched( input_item_t *p_item )
493 vlc_mutex_lock( &p_item->lock );
494 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
495 vlc_mutex_unlock( &p_item->lock );
500 static void input_item_Destroy ( gc_object_t *p_gc )
502 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
504 input_item_Clean( p_item );
508 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
511 int err = VLC_SUCCESS;
513 if( psz_option == NULL )
516 vlc_mutex_lock( &p_input->lock );
517 if (flags & VLC_INPUT_OPTION_UNIQUE)
519 for (int i = 0 ; i < p_input->i_options; i++)
520 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
524 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
530 p_input->optflagv = flagv;
531 flagv[p_input->optflagc++] = flags;
533 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
534 p_input->i_options, strdup( psz_option ) );
536 vlc_mutex_unlock( &p_input->lock );
541 * Get a info item from a given category in a given input item.
543 * \param p_i The input item to get info from
544 * \param psz_cat String representing the category for the info
545 * \param psz_name String representing the name of the desired info
546 * \return A pointer to the string with the given info if found, or an
547 * empty string otherwise. The caller should free the returned
550 char *input_item_GetInfo( input_item_t *p_i,
552 const char *psz_name )
554 vlc_mutex_lock( &p_i->lock );
556 for( int i = 0; i< p_i->i_categories; i++ )
558 const info_category_t *p_cat = p_i->pp_categories[i];
560 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
563 for( int j = 0; j < p_cat->i_infos; j++ )
565 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
567 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
568 vlc_mutex_unlock( &p_i->lock );
573 vlc_mutex_unlock( &p_i->lock );
577 static int InputItemVaAddInfo( input_item_t *p_i,
579 const char *psz_name,
580 const char *psz_format, va_list args )
583 info_t *p_info = NULL;
584 info_category_t *p_cat = NULL ;
586 vlc_assert_locked( &p_i->lock );
588 for( i = 0 ; i < p_i->i_categories ; i ++ )
590 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
592 p_cat = p_i->pp_categories[i];
598 if( !(p_cat = malloc( sizeof(*p_cat) )) )
601 p_cat->psz_name = strdup( psz_cat );
604 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
608 for( i = 0; i< p_cat->i_infos; i++ )
610 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
612 p_info = p_cat->pp_infos[i];
619 if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
622 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
623 p_info->psz_name = strdup( psz_name );
627 free( p_info->psz_value );
630 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
631 p_info->psz_value = NULL;
633 return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
636 static int InputItemAddInfo( input_item_t *p_i,
638 const char *psz_name,
639 const char *psz_format, ... )
643 va_start( args, psz_format );
644 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
650 int input_item_AddInfo( input_item_t *p_i,
652 const char *psz_name,
653 const char *psz_format, ... )
657 vlc_mutex_lock( &p_i->lock );
659 va_start( args, psz_format );
660 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
663 vlc_mutex_unlock( &p_i->lock );
670 event.type = vlc_InputItemInfoChanged;
671 vlc_event_send( &p_i->event_manager, &event );
676 int input_item_DelInfo( input_item_t *p_i,
678 const char *psz_name )
680 info_category_t *p_cat = NULL;
684 vlc_mutex_lock( &p_i->lock );
685 for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
687 if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
690 p_cat = p_i->pp_categories[i_cat];
696 vlc_mutex_unlock( &p_i->lock );
702 /* Remove a specific info */
703 for( i = 0; i < p_cat->i_infos; i++ )
705 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
707 free( p_cat->pp_infos[i]->psz_name );
708 free( p_cat->pp_infos[i]->psz_value );
709 free( p_cat->pp_infos[i] );
710 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
714 if( i >= p_cat->i_infos )
716 vlc_mutex_unlock( &p_i->lock );
722 /* Remove the complete categorie */
723 for( i = 0; i < p_cat->i_infos; i++ )
725 free( p_cat->pp_infos[i]->psz_name );
726 free( p_cat->pp_infos[i]->psz_value );
727 free( p_cat->pp_infos[i] );
729 free( p_cat->pp_infos );
730 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
734 free( p_cat->psz_name );
736 vlc_mutex_unlock( &p_i->lock );
740 event.type = vlc_InputItemInfoChanged;
741 vlc_event_send( &p_i->event_manager, &event );
747 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
749 vlc_mutex_lock( &p_item->lock );
752 vlc_epg_t *p_epg = NULL;
753 for( int i = 0; i < p_item->i_epg; i++ )
755 vlc_epg_t *p_tmp = p_item->pp_epg[i];
757 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
759 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
769 p_epg = vlc_epg_New( p_update->psz_name );
771 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
774 vlc_epg_Merge( p_epg, p_update );
776 vlc_mutex_unlock( &p_item->lock );
780 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
783 input_item_DelInfo( p_item, psz_epg, NULL );
785 vlc_mutex_lock( &p_item->lock );
786 for( int i = 0; i < p_epg->i_event; i++ )
788 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
789 time_t t_start = (time_t)p_evt->i_start;
793 localtime_r( &t_start, &tm_start );
795 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
796 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
797 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
798 if( p_evt->psz_short_description || p_evt->psz_description )
799 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
801 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
802 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
803 p_evt->psz_description ? p_evt->psz_description : "" );
805 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
807 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
809 vlc_mutex_unlock( &p_item->lock );
814 if( p_epg->i_event > 0 )
816 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
817 vlc_event_send( &p_item->event_manager, &event );
821 void input_item_SetEpgOffline( input_item_t *p_item )
823 vlc_mutex_lock( &p_item->lock );
824 for( int i = 0; i < p_item->i_epg; i++ )
825 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
826 vlc_mutex_unlock( &p_item->lock );
829 vlc_mutex_lock( &p_item->lock );
830 const int i_epg_info = p_item->i_epg;
831 char *ppsz_epg_info[i_epg_info];
832 for( int i = 0; i < p_item->i_epg; i++ )
834 const vlc_epg_t *p_epg = p_item->pp_epg[i];
835 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
836 ppsz_epg_info[i] = NULL;
838 vlc_mutex_unlock( &p_item->lock );
840 for( int i = 0; i < i_epg_info; i++ )
842 if( !ppsz_epg_info[i] )
844 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
845 free( ppsz_epg_info[i] );
849 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
850 vlc_event_send( &p_item->event_manager, &event );
854 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
855 const char *psz_name,
857 const char *const *ppsz_options,
858 unsigned i_option_flags,
861 return input_item_NewWithType( p_obj, psz_uri, psz_name,
862 i_options, ppsz_options, i_option_flags,
863 i_duration, ITEM_TYPE_UNKNOWN );
867 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
868 const char *psz_name,
870 const char *const *ppsz_options,
871 unsigned i_option_flags,
875 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
876 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
878 input_item_t* p_input = malloc( sizeof(input_item_t ) );
882 input_item_Init( p_obj, p_input );
883 vlc_gc_init( p_input, input_item_Destroy );
885 vlc_mutex_lock( &input_id_lock );
886 p_input->i_id = ++priv->i_last_input_id;
887 vlc_mutex_unlock( &input_id_lock );
889 p_input->b_fixed_name = false;
891 p_input->i_type = i_type;
892 p_input->b_prefers_tree = false;
895 input_item_SetURI( p_input, psz_uri );
897 if( i_type != ITEM_TYPE_UNKNOWN )
898 p_input->i_type = i_type;
901 input_item_SetName( p_input, psz_name );
903 p_input->i_duration = i_duration;
905 for( int i = 0; i < i_options; i++ )
906 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
910 struct item_type_entry
912 const char psz_scheme[7];
916 static int typecmp( const void *key, const void *entry )
918 const struct item_type_entry *type = entry;
919 const char *uri = key, *scheme = type->psz_scheme;
921 return strncmp( uri, scheme, strlen( scheme ) );
924 /* Guess the type of the item using the beginning of the mrl */
925 static int GuessType( const input_item_t *p_item )
927 static const struct item_type_entry tab[] =
928 { /* /!\ Alphabetical order /!\ */
929 /* Short match work, not just exact match */
930 { "alsa", ITEM_TYPE_CARD },
931 { "atsc", ITEM_TYPE_CARD },
932 { "bd", ITEM_TYPE_DISC },
933 { "cable", ITEM_TYPE_CARD },
934 { "cdda", ITEM_TYPE_CDDA },
935 { "dc1394", ITEM_TYPE_CARD },
936 { "dccp", ITEM_TYPE_NET },
937 { "dir", ITEM_TYPE_DIRECTORY },
938 { "dshow", ITEM_TYPE_CARD },
939 { "dv", ITEM_TYPE_CARD },
940 { "dvb", ITEM_TYPE_CARD },
941 { "dvd", ITEM_TYPE_DISC },
942 { "ftp", ITEM_TYPE_NET },
943 { "http", ITEM_TYPE_NET },
944 { "icyx", ITEM_TYPE_NET },
945 { "itpc", ITEM_TYPE_NET },
946 { "jack", ITEM_TYPE_CARD },
947 { "live", ITEM_TYPE_NET }, /* livedotcom */
948 { "mms", ITEM_TYPE_NET },
949 { "mtp", ITEM_TYPE_DISC },
950 { "ofdm", ITEM_TYPE_CARD },
951 { "oss", ITEM_TYPE_CARD },
952 { "pnm", ITEM_TYPE_NET },
953 { "pvr", ITEM_TYPE_CARD },
954 { "qam", ITEM_TYPE_CARD },
955 { "qpsk", ITEM_TYPE_CARD },
956 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
957 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
958 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
959 { "satell", ITEM_TYPE_CARD }, /* sattelite */
960 { "screen", ITEM_TYPE_CARD },
961 { "sdp", ITEM_TYPE_NET },
962 { "smb", ITEM_TYPE_NET },
963 { "svcd", ITEM_TYPE_DISC },
964 { "tcp", ITEM_TYPE_NET },
965 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
966 { "udp", ITEM_TYPE_NET }, /* udplite too */
967 { "unsv", ITEM_TYPE_NET },
968 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
969 { "v4l", ITEM_TYPE_CARD },
970 { "vcd", ITEM_TYPE_DISC },
971 { "window", ITEM_TYPE_CARD },
973 const struct item_type_entry *e;
975 if( !strstr( p_item->psz_uri, "://" ) )
976 return ITEM_TYPE_FILE;
978 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
979 sizeof( tab[0] ), typecmp );
980 return e ? e->i_type : ITEM_TYPE_FILE;
983 input_item_node_t *input_item_node_Create( input_item_t *p_input )
985 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
991 p_node->p_item = p_input;
992 vlc_gc_incref( p_input );
994 p_node->p_parent = NULL;
995 p_node->i_children = 0;
996 p_node->pp_children = NULL;
1001 void input_item_node_Delete( input_item_node_t *p_node )
1004 for( i = 0; i < p_node->i_children; i++ )
1005 input_item_node_Delete( p_node->pp_children[i] );
1007 if( p_node->p_parent )
1009 for( i = 0; i < p_node->p_parent->i_children; i++ )
1010 if( p_node->p_parent->pp_children[i] == p_node )
1012 REMOVE_ELEM( p_node->p_parent->pp_children,
1013 p_node->p_parent->i_children,
1019 vlc_gc_decref( p_node->p_item );
1023 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1025 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1026 if( !p_new_child ) return NULL;
1027 input_item_node_AppendNode( p_node, p_new_child );
1031 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1033 assert( p_parent && p_child && p_child->p_parent == NULL );
1034 INSERT_ELEM( p_parent->pp_children,
1035 p_parent->i_children,
1036 p_parent->i_children,
1038 p_child->p_parent = p_parent;