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 *****************************************************************************/
30 #include <vlc_common.h>
32 #include "vlc_playlist.h"
33 #include "vlc_interface.h"
34 #include <vlc_charset.h>
35 #include <vlc_atomic.h>
40 static int GuessType( const input_item_t *p_item );
42 static inline void input_item_Clean( input_item_t *p_i )
46 vlc_event_manager_fini( &p_i->event_manager );
48 free( p_i->psz_name );
52 vlc_mutex_destroy( &p_i->p_stats->lock );
57 vlc_meta_Delete( p_i->p_meta );
59 for( i = 0; i < p_i->i_options; i++ )
60 free( p_i->ppsz_options[i] );
61 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
64 for( i = 0; i < p_i->i_es; i++ )
66 es_format_Clean( p_i->es[i] );
69 TAB_CLEAN( p_i->i_es, p_i->es );
71 for( i = 0; i < p_i->i_epg; i++ )
72 vlc_epg_Delete( p_i->pp_epg[i] );
73 TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
75 for( i = 0; i < p_i->i_categories; i++ )
76 info_category_Delete( p_i->pp_categories[i] );
77 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
79 vlc_mutex_destroy( &p_i->lock );
81 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
85 vlc_mutex_lock( &p_i->lock );
87 b_changed = p_i->b_error_when_reading != b_error;
88 p_i->b_error_when_reading = b_error;
90 vlc_mutex_unlock( &p_i->lock );
96 event.type = vlc_InputItemErrorWhenReadingChanged;
97 event.u.input_item_error_when_reading_changed.new_value = b_error;
98 vlc_event_send( &p_i->event_manager, &event );
101 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
103 bool b_send_event = false;
105 vlc_mutex_lock( &p_i->lock );
108 p_i->p_meta = vlc_meta_New();
110 int status = vlc_meta_GetStatus(p_i->p_meta);
113 new_status = status | ITEM_PREPARSED;
115 new_status = status & ~ITEM_PREPARSED;
116 if( status != new_status )
118 vlc_meta_SetStatus(p_i->p_meta, new_status);
122 vlc_mutex_unlock( &p_i->lock );
127 event.type = vlc_InputItemPreparsedChanged;
128 event.u.input_item_preparsed_changed.new_status = new_status;
129 vlc_event_send( &p_i->event_manager, &event );
133 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
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 status |= ITEM_ART_NOTFOUND;
145 status &= ~ITEM_ART_NOTFOUND;
147 vlc_meta_SetStatus(p_i->p_meta, status);
149 vlc_mutex_unlock( &p_i->lock );
152 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
154 vlc_mutex_lock( &p_i->lock );
157 p_i->p_meta = vlc_meta_New();
159 int status = vlc_meta_GetStatus(p_i->p_meta);
162 status |= ITEM_ART_FETCHED;
164 status &= ~ITEM_ART_FETCHED;
166 vlc_meta_SetStatus(p_i->p_meta, status);
168 vlc_mutex_unlock( &p_i->lock );
171 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
175 vlc_mutex_lock( &p_i->lock );
177 p_i->p_meta = vlc_meta_New();
178 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
179 vlc_mutex_unlock( &p_i->lock );
181 /* Notify interested third parties */
182 event.type = vlc_InputItemMetaChanged;
183 event.u.input_item_meta_changed.meta_type = meta_type;
184 vlc_event_send( &p_i->event_manager, &event );
187 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
188 * consistent with (nearly?) all or copy funcs */
189 void input_item_CopyOptions( input_item_t *p_parent,
190 input_item_t *p_child )
192 vlc_mutex_lock( &p_parent->lock );
194 for( int i = 0 ; i< p_parent->i_options; i++ )
196 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
199 input_item_AddOption( p_child,
200 p_parent->ppsz_options[i],
201 p_parent->optflagv[i] );
204 vlc_mutex_unlock( &p_parent->lock );
207 static void post_subitems( input_item_node_t *p_node )
209 for( int i = 0; i < p_node->i_children; i++ )
212 event.type = vlc_InputItemSubItemAdded;
213 event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
214 vlc_event_send( &p_node->p_item->event_manager, &event );
216 post_subitems( p_node->pp_children[i] );
220 /* This won't hold the item, but can tell to interested third parties
221 * Like the playlist, that there is a new sub item. With this design
222 * It is not the input item's responsability to keep all the ref of
223 * the input item children. */
224 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
226 input_item_node_t *p_node = input_item_node_Create( p_parent );
227 input_item_node_AppendItem( p_node, p_child );
228 input_item_node_PostAndDelete( p_node );
231 bool input_item_HasErrorWhenReading( input_item_t *p_item )
233 vlc_mutex_lock( &p_item->lock );
235 bool b_error = p_item->b_error_when_reading;
237 vlc_mutex_unlock( &p_item->lock );
242 bool input_item_MetaMatch( input_item_t *p_i,
243 vlc_meta_type_t meta_type, const char *psz )
245 vlc_mutex_lock( &p_i->lock );
249 vlc_mutex_unlock( &p_i->lock );
252 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
253 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
255 vlc_mutex_unlock( &p_i->lock );
260 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
262 vlc_mutex_lock( &p_i->lock );
266 vlc_mutex_unlock( &p_i->lock );
271 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
272 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
274 vlc_mutex_unlock( &p_i->lock );
278 /* Get the title of a given item or fallback to the name if the title is empty */
279 char *input_item_GetTitleFbName( input_item_t *p_item )
282 vlc_mutex_lock( &p_item->lock );
284 if( !p_item->p_meta )
286 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
287 vlc_mutex_unlock( &p_item->lock );
291 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
292 if( !EMPTY_STR( psz_title ) )
293 psz_ret = strdup( psz_title );
295 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
297 vlc_mutex_unlock( &p_item->lock );
301 char *input_item_GetName( input_item_t *p_item )
303 vlc_mutex_lock( &p_item->lock );
305 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
307 vlc_mutex_unlock( &p_item->lock );
310 void input_item_SetName( input_item_t *p_item, const char *psz_name )
312 vlc_mutex_lock( &p_item->lock );
314 free( p_item->psz_name );
315 p_item->psz_name = strdup( psz_name );
317 vlc_mutex_unlock( &p_item->lock );
320 char *input_item_GetURI( input_item_t *p_i )
322 vlc_mutex_lock( &p_i->lock );
324 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
326 vlc_mutex_unlock( &p_i->lock );
330 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
334 if( !strstr( psz_uri, "://" )
335 || strchr( psz_uri, ' ' ) || strchr( psz_uri, '"' ) )
336 fprintf( stderr, "Warning: %s(\"%s\"): file path instead of URL.\n",
339 vlc_mutex_lock( &p_i->lock );
340 free( p_i->psz_uri );
341 p_i->psz_uri = strdup( psz_uri );
343 p_i->i_type = GuessType( p_i );
348 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
350 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
352 if( psz_filename && *psz_filename == '/' )
354 if( psz_filename && *psz_filename )
355 p_i->psz_name = strdup( psz_filename );
357 /* Make the name more readable */
360 decode_URI( p_i->psz_name );
361 EnsureUTF8( p_i->psz_name );
365 { /* Strip login and password from title */
369 vlc_UrlParse( &url, psz_uri, 0 );
370 if( url.psz_protocol )
373 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
374 url.psz_host, url.i_port,
375 url.psz_path ? url.psz_path : "" );
377 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
378 url.psz_host ? url.psz_host : "",
379 url.psz_path ? url.psz_path : "" );
384 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
385 url.psz_path ? url.psz_path : "" );
387 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
388 url.psz_path ? url.psz_path : "" );
390 vlc_UrlClean( &url );
392 p_i->psz_name=NULL; /* recover from undefined value */
395 vlc_mutex_unlock( &p_i->lock );
398 mtime_t input_item_GetDuration( input_item_t *p_i )
400 vlc_mutex_lock( &p_i->lock );
402 mtime_t i_duration = p_i->i_duration;
404 vlc_mutex_unlock( &p_i->lock );
408 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
410 bool b_send_event = false;
412 vlc_mutex_lock( &p_i->lock );
413 if( p_i->i_duration != i_duration )
415 p_i->i_duration = i_duration;
418 vlc_mutex_unlock( &p_i->lock );
424 event.type = vlc_InputItemDurationChanged;
425 event.u.input_item_duration_changed.new_duration = i_duration;
426 vlc_event_send( &p_i->event_manager, &event );
431 bool input_item_IsPreparsed( input_item_t *p_item )
433 vlc_mutex_lock( &p_item->lock );
434 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
435 vlc_mutex_unlock( &p_item->lock );
440 bool input_item_IsArtFetched( input_item_t *p_item )
442 vlc_mutex_lock( &p_item->lock );
443 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
444 vlc_mutex_unlock( &p_item->lock );
449 static void input_item_Destroy ( gc_object_t *p_gc )
451 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
453 input_item_Clean( p_item );
457 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
460 int err = VLC_SUCCESS;
462 if( psz_option == NULL )
465 vlc_mutex_lock( &p_input->lock );
466 if (flags & VLC_INPUT_OPTION_UNIQUE)
468 for (int i = 0 ; i < p_input->i_options; i++)
469 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
473 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
479 p_input->optflagv = flagv;
480 flagv[p_input->optflagc++] = flags;
482 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
483 p_input->i_options, strdup( psz_option ) );
485 vlc_mutex_unlock( &p_input->lock );
489 static info_category_t *InputItemFindCat( input_item_t *p_item,
490 int *pi_index, const char *psz_cat )
492 vlc_assert_locked( &p_item->lock );
493 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
495 info_category_t *p_cat = p_item->pp_categories[i];
497 if( !strcmp( p_cat->psz_name, psz_cat ) )
508 * Get a info item from a given category in a given input item.
510 * \param p_i The input item to get info from
511 * \param psz_cat String representing the category for the info
512 * \param psz_name String representing the name of the desired info
513 * \return A pointer to the string with the given info if found, or an
514 * empty string otherwise. The caller should free the returned
517 char *input_item_GetInfo( input_item_t *p_i,
519 const char *psz_name )
521 vlc_mutex_lock( &p_i->lock );
523 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
526 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
527 if( p_info && p_info->psz_value )
529 char *psz_ret = strdup( p_info->psz_value );
530 vlc_mutex_unlock( &p_i->lock );
534 vlc_mutex_unlock( &p_i->lock );
538 static int InputItemVaAddInfo( input_item_t *p_i,
540 const char *psz_name,
541 const char *psz_format, va_list args )
543 vlc_assert_locked( &p_i->lock );
545 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
548 p_cat = info_category_New( psz_cat );
551 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
554 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
555 if( !p_info || !p_info->psz_value )
560 static int InputItemAddInfo( input_item_t *p_i,
562 const char *psz_name,
563 const char *psz_format, ... )
567 va_start( args, psz_format );
568 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
574 int input_item_AddInfo( input_item_t *p_i,
576 const char *psz_name,
577 const char *psz_format, ... )
581 vlc_mutex_lock( &p_i->lock );
583 va_start( args, psz_format );
584 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
587 vlc_mutex_unlock( &p_i->lock );
594 event.type = vlc_InputItemInfoChanged;
595 vlc_event_send( &p_i->event_manager, &event );
600 int input_item_DelInfo( input_item_t *p_i,
602 const char *psz_name )
604 vlc_mutex_lock( &p_i->lock );
606 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
609 vlc_mutex_unlock( &p_i->lock );
615 /* Remove a specific info */
616 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
619 vlc_mutex_unlock( &p_i->lock );
625 /* Remove the complete categorie */
626 info_category_Delete( p_cat );
627 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
629 vlc_mutex_unlock( &p_i->lock );
633 event.type = vlc_InputItemInfoChanged;
634 vlc_event_send( &p_i->event_manager, &event );
638 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
640 vlc_mutex_lock( &p_item->lock );
642 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
645 info_category_Delete( p_old );
646 p_item->pp_categories[i_cat] = p_cat;
650 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
653 vlc_mutex_unlock( &p_item->lock );
657 event.type = vlc_InputItemInfoChanged;
658 vlc_event_send( &p_item->event_manager, &event );
660 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
662 vlc_mutex_lock( &p_item->lock );
663 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
666 for( int i = 0; i < p_cat->i_infos; i++ )
667 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
668 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
669 info_category_Delete( p_cat );
673 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
676 vlc_mutex_unlock( &p_item->lock );
680 event.type = vlc_InputItemInfoChanged;
681 vlc_event_send( &p_item->event_manager, &event );
685 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
687 vlc_mutex_lock( &p_item->lock );
690 vlc_epg_t *p_epg = NULL;
691 for( int i = 0; i < p_item->i_epg; i++ )
693 vlc_epg_t *p_tmp = p_item->pp_epg[i];
695 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
697 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
707 p_epg = vlc_epg_New( p_update->psz_name );
709 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
712 vlc_epg_Merge( p_epg, p_update );
714 vlc_mutex_unlock( &p_item->lock );
721 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
724 input_item_DelInfo( p_item, psz_epg, NULL );
726 vlc_mutex_lock( &p_item->lock );
727 for( int i = 0; i < p_epg->i_event; i++ )
729 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
730 time_t t_start = (time_t)p_evt->i_start;
734 localtime_r( &t_start, &tm_start );
736 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
737 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
738 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
739 if( p_evt->psz_short_description || p_evt->psz_description )
740 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
742 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
743 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
744 p_evt->psz_description ? p_evt->psz_description : "" );
746 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
748 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
750 vlc_mutex_unlock( &p_item->lock );
755 if( p_epg->i_event > 0 )
757 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
758 vlc_event_send( &p_item->event_manager, &event );
762 void input_item_SetEpgOffline( input_item_t *p_item )
764 vlc_mutex_lock( &p_item->lock );
765 for( int i = 0; i < p_item->i_epg; i++ )
766 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
767 vlc_mutex_unlock( &p_item->lock );
770 vlc_mutex_lock( &p_item->lock );
771 const int i_epg_info = p_item->i_epg;
772 char *ppsz_epg_info[i_epg_info];
773 for( int i = 0; i < p_item->i_epg; i++ )
775 const vlc_epg_t *p_epg = p_item->pp_epg[i];
776 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
777 ppsz_epg_info[i] = NULL;
779 vlc_mutex_unlock( &p_item->lock );
781 for( int i = 0; i < i_epg_info; i++ )
783 if( !ppsz_epg_info[i] )
785 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
786 free( ppsz_epg_info[i] );
790 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
791 vlc_event_send( &p_item->event_manager, &event );
794 input_item_t *input_item_NewExt( const char *psz_uri,
795 const char *psz_name,
797 const char *const *ppsz_options,
798 unsigned i_option_flags,
801 return input_item_NewWithType( psz_uri, psz_name,
802 i_options, ppsz_options, i_option_flags,
803 i_duration, ITEM_TYPE_UNKNOWN );
808 input_item_NewWithType( const char *psz_uri, const char *psz_name,
809 int i_options, const char *const *ppsz_options,
810 unsigned flags, mtime_t duration, int type )
812 static vlc_atomic_t last_input_id = VLC_ATOMIC_INIT(0);
814 input_item_t* p_input = calloc( 1, sizeof( *p_input ) );
817 vlc_event_manager_t * p_em = &p_input->event_manager;
819 p_input->i_id = vlc_atomic_inc(&last_input_id);
820 vlc_gc_init( p_input, input_item_Destroy );
821 vlc_mutex_init( &p_input->lock );
823 p_input->psz_name = NULL;
825 input_item_SetName( p_input, psz_name );
827 p_input->psz_uri = NULL;
829 input_item_SetURI( p_input, psz_uri );
831 p_input->i_type = ITEM_TYPE_UNKNOWN;
833 TAB_INIT( p_input->i_options, p_input->ppsz_options );
834 p_input->optflagc = 0;
835 p_input->optflagv = NULL;
836 for( int i = 0; i < i_options; i++ )
837 input_item_AddOption( p_input, ppsz_options[i], flags );
839 p_input->i_duration = duration;
840 TAB_INIT( p_input->i_categories, p_input->pp_categories );
841 TAB_INIT( p_input->i_es, p_input->es );
842 p_input->p_stats = NULL;
843 p_input->i_nb_played = 0;
844 p_input->p_meta = NULL;
845 TAB_INIT( p_input->i_epg, p_input->pp_epg );
847 vlc_event_manager_init( p_em, p_input );
848 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
849 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
850 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
851 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
852 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
853 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
854 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
855 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
857 if( type != ITEM_TYPE_UNKNOWN )
858 p_input->i_type = type;
859 p_input->b_fixed_name = false;
860 p_input->b_error_when_reading = false;
864 input_item_t *input_item_Copy( input_item_t *p_input )
866 vlc_mutex_lock( &p_input->lock );
868 input_item_t *p_new_input =
869 input_item_NewWithType( p_input->psz_uri, p_input->psz_name,
870 0, NULL, 0, p_input->i_duration,
875 for( int i = 0 ; i< p_input->i_options; i++ )
877 input_item_AddOption( p_new_input,
878 p_input->ppsz_options[i],
879 p_input->optflagv[i] );
882 if( p_input->p_meta )
884 p_new_input->p_meta = vlc_meta_New();
885 vlc_meta_Merge( p_new_input->p_meta, p_input->p_meta );
889 vlc_mutex_unlock( &p_input->lock );
894 struct item_type_entry
896 const char psz_scheme[7];
900 static int typecmp( const void *key, const void *entry )
902 const struct item_type_entry *type = entry;
903 const char *uri = key, *scheme = type->psz_scheme;
905 return strncmp( uri, scheme, strlen( scheme ) );
908 /* Guess the type of the item using the beginning of the mrl */
909 static int GuessType( const input_item_t *p_item )
911 static const struct item_type_entry tab[] =
912 { /* /!\ Alphabetical order /!\ */
913 /* Short match work, not just exact match */
914 { "alsa", ITEM_TYPE_CARD },
915 { "atsc", ITEM_TYPE_CARD },
916 { "bd", ITEM_TYPE_DISC },
917 { "cable", ITEM_TYPE_CARD },
918 { "cdda", ITEM_TYPE_CDDA },
919 { "cqam", ITEM_TYPE_CARD },
920 { "dc1394", ITEM_TYPE_CARD },
921 { "dccp", ITEM_TYPE_NET },
922 { "deckli", ITEM_TYPE_CARD }, /* decklink */
923 { "dir", ITEM_TYPE_DIRECTORY },
924 { "dshow", ITEM_TYPE_CARD },
925 { "dv", ITEM_TYPE_CARD },
926 { "dvb", ITEM_TYPE_CARD },
927 { "dvd", ITEM_TYPE_DISC },
928 { "dtv", ITEM_TYPE_CARD },
929 { "eyetv", ITEM_TYPE_CARD },
930 { "fd", ITEM_TYPE_UNKNOWN },
931 { "ftp", ITEM_TYPE_NET },
932 { "http", ITEM_TYPE_NET },
933 { "icyx", ITEM_TYPE_NET },
934 { "imem", ITEM_TYPE_UNKNOWN },
935 { "itpc", ITEM_TYPE_NET },
936 { "jack", ITEM_TYPE_CARD },
937 { "linsys", ITEM_TYPE_CARD },
938 { "live", ITEM_TYPE_NET }, /* livedotcom */
939 { "mms", ITEM_TYPE_NET },
940 { "mtp", ITEM_TYPE_DISC },
941 { "ofdm", ITEM_TYPE_CARD },
942 { "oss", ITEM_TYPE_CARD },
943 { "pnm", ITEM_TYPE_NET },
944 { "pvr", ITEM_TYPE_CARD },
945 { "qam", ITEM_TYPE_CARD },
946 { "qpsk", ITEM_TYPE_CARD },
947 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
948 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
949 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
950 { "satell", ITEM_TYPE_CARD }, /* sattelite */
951 { "screen", ITEM_TYPE_CARD },
952 { "sdp", ITEM_TYPE_NET },
953 { "sftp", ITEM_TYPE_NET },
954 { "shm", ITEM_TYPE_CARD },
955 { "smb", ITEM_TYPE_NET },
956 { "svcd", ITEM_TYPE_DISC },
957 { "tcp", ITEM_TYPE_NET },
958 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
959 { "udp", ITEM_TYPE_NET }, /* udplite too */
960 { "unsv", ITEM_TYPE_NET },
961 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
962 { "v4l", ITEM_TYPE_CARD },
963 { "vcd", ITEM_TYPE_DISC },
964 { "window", ITEM_TYPE_CARD },
966 const struct item_type_entry *e;
968 if( !strstr( p_item->psz_uri, "://" ) )
969 return ITEM_TYPE_FILE;
971 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
972 sizeof( tab[0] ), typecmp );
973 return e ? e->i_type : ITEM_TYPE_FILE;
976 input_item_node_t *input_item_node_Create( input_item_t *p_input )
978 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
984 p_node->p_item = p_input;
985 vlc_gc_incref( p_input );
987 p_node->p_parent = NULL;
988 p_node->i_children = 0;
989 p_node->pp_children = NULL;
994 static void RecursiveNodeDelete( input_item_node_t *p_node )
996 for( int i = 0; i < p_node->i_children; i++ )
997 RecursiveNodeDelete( p_node->pp_children[i] );
999 vlc_gc_decref( p_node->p_item );
1000 free( p_node->pp_children );
1004 void input_item_node_Delete( input_item_node_t *p_node )
1006 if( p_node->p_parent )
1007 for( int i = 0; i < p_node->p_parent->i_children; i++ )
1008 if( p_node->p_parent->pp_children[i] == p_node )
1010 REMOVE_ELEM( p_node->p_parent->pp_children,
1011 p_node->p_parent->i_children,
1016 RecursiveNodeDelete( p_node );
1019 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1021 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1022 if( !p_new_child ) return NULL;
1023 input_item_node_AppendNode( p_node, p_new_child );
1027 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1029 assert( p_parent && p_child && p_child->p_parent == NULL );
1030 INSERT_ELEM( p_parent->pp_children,
1031 p_parent->i_children,
1032 p_parent->i_children,
1034 p_child->p_parent = p_parent;
1037 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1039 post_subitems( p_root );
1042 event.type = vlc_InputItemSubItemTreeAdded;
1043 event.u.input_item_subitem_tree_added.p_root = p_root;
1044 vlc_event_send( &p_root->p_item->event_manager, &event );
1046 input_item_node_Delete( p_root );
1049 /* Called by es_out when a new Elementary Stream is added or updated. */
1050 void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
1053 es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
1057 es_format_Copy(fmt_copy, fmt);
1058 /* XXX: we could free p_extra to save memory, we will likely not need
1059 * the decoder specific data */
1061 vlc_mutex_lock( &item->lock );
1063 for( i = 0; i < item->i_es; i++ )
1065 if (item->es[i]->i_id != fmt->i_id)
1068 /* We've found the right ES, replace it */
1069 es_format_Clean(item->es[i]);
1071 item->es[i] = fmt_copy;
1072 vlc_mutex_unlock( &item->lock );
1076 /* ES not found, insert it */
1077 TAB_APPEND(item->i_es, item->es, fmt_copy);
1078 vlc_mutex_unlock( &item->lock );