1 /*****************************************************************************
2 * item.c: input_item management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include <vlc_common.h>
32 #include <vlc_interface.h>
33 #include <vlc_charset.h>
38 static int GuessType( const input_item_t *p_item, bool *p_net );
40 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
44 vlc_mutex_lock( &p_i->lock );
46 b_changed = p_i->b_error_when_reading != b_error;
47 p_i->b_error_when_reading = b_error;
49 vlc_mutex_unlock( &p_i->lock );
55 event.type = vlc_InputItemErrorWhenReadingChanged;
56 event.u.input_item_error_when_reading_changed.new_value = b_error;
57 vlc_event_send( &p_i->event_manager, &event );
60 void input_item_SignalPreparseEnded( input_item_t *p_i )
63 event.type = vlc_InputItemPreparseEnded;
64 vlc_event_send( &p_i->event_manager, &event );
67 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
69 bool b_send_event = false;
71 vlc_mutex_lock( &p_i->lock );
74 p_i->p_meta = vlc_meta_New();
76 int status = vlc_meta_GetStatus(p_i->p_meta);
79 new_status = status | ITEM_PREPARSED;
81 new_status = status & ~ITEM_PREPARSED;
82 if( status != new_status )
84 vlc_meta_SetStatus(p_i->p_meta, new_status);
88 vlc_mutex_unlock( &p_i->lock );
93 event.type = vlc_InputItemPreparsedChanged;
94 event.u.input_item_preparsed_changed.new_status = new_status;
95 vlc_event_send( &p_i->event_manager, &event );
99 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
101 vlc_mutex_lock( &p_i->lock );
104 p_i->p_meta = vlc_meta_New();
106 int status = vlc_meta_GetStatus(p_i->p_meta);
109 status |= ITEM_ART_NOTFOUND;
111 status &= ~ITEM_ART_NOTFOUND;
113 vlc_meta_SetStatus(p_i->p_meta, status);
115 vlc_mutex_unlock( &p_i->lock );
118 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
120 vlc_mutex_lock( &p_i->lock );
123 p_i->p_meta = vlc_meta_New();
125 int status = vlc_meta_GetStatus(p_i->p_meta);
128 status |= ITEM_ART_FETCHED;
130 status &= ~ITEM_ART_FETCHED;
132 vlc_meta_SetStatus(p_i->p_meta, status);
134 vlc_mutex_unlock( &p_i->lock );
137 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
141 vlc_mutex_lock( &p_i->lock );
143 p_i->p_meta = vlc_meta_New();
144 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
145 vlc_mutex_unlock( &p_i->lock );
147 /* Notify interested third parties */
148 event.type = vlc_InputItemMetaChanged;
149 event.u.input_item_meta_changed.meta_type = meta_type;
150 vlc_event_send( &p_i->event_manager, &event );
153 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
154 * consistent with (nearly?) all or copy funcs */
155 void input_item_CopyOptions( input_item_t *p_parent,
156 input_item_t *p_child )
158 vlc_mutex_lock( &p_parent->lock );
160 for( int i = 0 ; i< p_parent->i_options; i++ )
162 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
165 input_item_AddOption( p_child,
166 p_parent->ppsz_options[i],
167 p_parent->optflagv[i] );
170 vlc_mutex_unlock( &p_parent->lock );
173 static void post_subitems( input_item_node_t *p_node )
175 for( int i = 0; i < p_node->i_children; i++ )
178 event.type = vlc_InputItemSubItemAdded;
179 event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
180 vlc_event_send( &p_node->p_item->event_manager, &event );
182 post_subitems( p_node->pp_children[i] );
186 /* This won't hold the item, but can tell to interested third parties
187 * Like the playlist, that there is a new sub item. With this design
188 * It is not the input item's responsability to keep all the ref of
189 * the input item children. */
190 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
192 input_item_node_t *p_node = input_item_node_Create( p_parent );
193 input_item_node_AppendItem( p_node, p_child );
194 input_item_node_PostAndDelete( p_node );
197 bool input_item_HasErrorWhenReading( input_item_t *p_item )
199 vlc_mutex_lock( &p_item->lock );
201 bool b_error = p_item->b_error_when_reading;
203 vlc_mutex_unlock( &p_item->lock );
208 bool input_item_MetaMatch( input_item_t *p_i,
209 vlc_meta_type_t meta_type, const char *psz )
211 vlc_mutex_lock( &p_i->lock );
215 vlc_mutex_unlock( &p_i->lock );
218 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
219 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
221 vlc_mutex_unlock( &p_i->lock );
226 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
228 vlc_mutex_lock( &p_i->lock );
232 vlc_mutex_unlock( &p_i->lock );
237 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
238 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
240 vlc_mutex_unlock( &p_i->lock );
244 /* Get the title of a given item or fallback to the name if the title is empty */
245 char *input_item_GetTitleFbName( input_item_t *p_item )
248 vlc_mutex_lock( &p_item->lock );
250 if( !p_item->p_meta )
252 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
253 vlc_mutex_unlock( &p_item->lock );
257 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
258 if( !EMPTY_STR( psz_title ) )
259 psz_ret = strdup( psz_title );
261 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
263 vlc_mutex_unlock( &p_item->lock );
267 char *input_item_GetName( input_item_t *p_item )
269 vlc_mutex_lock( &p_item->lock );
271 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
273 vlc_mutex_unlock( &p_item->lock );
276 void input_item_SetName( input_item_t *p_item, const char *psz_name )
278 vlc_mutex_lock( &p_item->lock );
280 free( p_item->psz_name );
281 p_item->psz_name = strdup( psz_name );
283 vlc_mutex_unlock( &p_item->lock );
286 char *input_item_GetURI( input_item_t *p_i )
288 vlc_mutex_lock( &p_i->lock );
290 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
292 vlc_mutex_unlock( &p_i->lock );
296 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
300 if( !strstr( psz_uri, "://" )
301 || strchr( psz_uri, ' ' ) || strchr( psz_uri, '"' ) )
302 fprintf( stderr, "Warning: %s(\"%s\"): file path instead of URL.\n",
305 vlc_mutex_lock( &p_i->lock );
306 free( p_i->psz_uri );
307 p_i->psz_uri = strdup( psz_uri );
309 p_i->i_type = GuessType( p_i, &p_i->b_net );
314 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
316 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
318 if( psz_filename && *psz_filename == '/' )
320 if( psz_filename && *psz_filename )
321 p_i->psz_name = strdup( psz_filename );
323 /* Make the name more readable */
326 decode_URI( p_i->psz_name );
327 EnsureUTF8( p_i->psz_name );
331 { /* Strip login and password from title */
335 vlc_UrlParse( &url, psz_uri, 0 );
336 if( url.psz_protocol )
339 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
340 url.psz_host, url.i_port,
341 url.psz_path ? url.psz_path : "" );
343 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
344 url.psz_host ? url.psz_host : "",
345 url.psz_path ? url.psz_path : "" );
350 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
351 url.psz_path ? url.psz_path : "" );
353 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
354 url.psz_path ? url.psz_path : "" );
356 vlc_UrlClean( &url );
358 p_i->psz_name=NULL; /* recover from undefined value */
361 vlc_mutex_unlock( &p_i->lock );
364 mtime_t input_item_GetDuration( input_item_t *p_i )
366 vlc_mutex_lock( &p_i->lock );
368 mtime_t i_duration = p_i->i_duration;
370 vlc_mutex_unlock( &p_i->lock );
374 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
376 bool b_send_event = false;
378 vlc_mutex_lock( &p_i->lock );
379 if( p_i->i_duration != i_duration )
381 p_i->i_duration = i_duration;
384 vlc_mutex_unlock( &p_i->lock );
390 event.type = vlc_InputItemDurationChanged;
391 event.u.input_item_duration_changed.new_duration = i_duration;
392 vlc_event_send( &p_i->event_manager, &event );
396 char *input_item_GetNowPlayingFb( input_item_t *p_item )
398 char *psz_meta = input_item_GetMeta( p_item, vlc_meta_NowPlaying );
399 if( !psz_meta || strlen( psz_meta ) == 0 )
402 return input_item_GetMeta( p_item, vlc_meta_ESNowPlaying );
408 bool input_item_IsPreparsed( input_item_t *p_item )
410 vlc_mutex_lock( &p_item->lock );
411 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
412 vlc_mutex_unlock( &p_item->lock );
417 bool input_item_IsArtFetched( input_item_t *p_item )
419 vlc_mutex_lock( &p_item->lock );
420 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
421 vlc_mutex_unlock( &p_item->lock );
426 bool input_item_ShouldPreparseSubItems( input_item_t *p_item )
430 vlc_mutex_lock( &p_item->lock );
431 b_ret = p_item->i_preparse_depth == -1 ? true : p_item->i_preparse_depth > 0;
432 vlc_mutex_unlock( &p_item->lock );
437 input_item_t *input_item_Hold( input_item_t *p_item )
439 input_item_owner_t *owner = item_owner(p_item);
441 atomic_fetch_add( &owner->refs, 1 );
445 void input_item_Release( input_item_t *p_item )
447 input_item_owner_t *owner = item_owner(p_item);
449 if( atomic_fetch_sub(&owner->refs, 1) != 1 )
452 vlc_event_manager_fini( &p_item->event_manager );
454 free( p_item->psz_name );
455 free( p_item->psz_uri );
456 if( p_item->p_stats != NULL )
458 vlc_mutex_destroy( &p_item->p_stats->lock );
459 free( p_item->p_stats );
462 if( p_item->p_meta != NULL )
463 vlc_meta_Delete( p_item->p_meta );
465 for( int i = 0; i < p_item->i_options; i++ )
466 free( p_item->ppsz_options[i] );
467 TAB_CLEAN( p_item->i_options, p_item->ppsz_options );
468 free( p_item->optflagv );
470 for( int i = 0; i < p_item->i_es; i++ )
472 es_format_Clean( p_item->es[i] );
473 free( p_item->es[i] );
475 TAB_CLEAN( p_item->i_es, p_item->es );
477 for( int i = 0; i < p_item->i_epg; i++ )
478 vlc_epg_Delete( p_item->pp_epg[i] );
479 TAB_CLEAN( p_item->i_epg, p_item->pp_epg );
481 for( int i = 0; i < p_item->i_categories; i++ )
482 info_category_Delete( p_item->pp_categories[i] );
483 TAB_CLEAN( p_item->i_categories, p_item->pp_categories );
485 vlc_mutex_destroy( &p_item->lock );
489 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
492 int err = VLC_SUCCESS;
494 if( psz_option == NULL )
497 vlc_mutex_lock( &p_input->lock );
498 if (flags & VLC_INPUT_OPTION_UNIQUE)
500 for (int i = 0 ; i < p_input->i_options; i++)
501 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
505 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
511 p_input->optflagv = flagv;
512 flagv[p_input->optflagc++] = flags;
514 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
515 p_input->i_options, strdup( psz_option ) );
517 vlc_mutex_unlock( &p_input->lock );
521 static info_category_t *InputItemFindCat( input_item_t *p_item,
522 int *pi_index, const char *psz_cat )
524 vlc_assert_locked( &p_item->lock );
525 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
527 info_category_t *p_cat = p_item->pp_categories[i];
529 if( !strcmp( p_cat->psz_name, psz_cat ) )
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 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
558 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
559 if( p_info && p_info->psz_value )
561 char *psz_ret = strdup( p_info->psz_value );
562 vlc_mutex_unlock( &p_i->lock );
566 vlc_mutex_unlock( &p_i->lock );
570 static int InputItemVaAddInfo( input_item_t *p_i,
572 const char *psz_name,
573 const char *psz_format, va_list args )
575 vlc_assert_locked( &p_i->lock );
577 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
580 p_cat = info_category_New( psz_cat );
583 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
586 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
587 if( !p_info || !p_info->psz_value )
592 static int InputItemAddInfo( input_item_t *p_i,
594 const char *psz_name,
595 const char *psz_format, ... )
599 va_start( args, psz_format );
600 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
606 int input_item_AddInfo( input_item_t *p_i,
608 const char *psz_name,
609 const char *psz_format, ... )
613 vlc_mutex_lock( &p_i->lock );
615 va_start( args, psz_format );
616 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
619 vlc_mutex_unlock( &p_i->lock );
626 event.type = vlc_InputItemInfoChanged;
627 vlc_event_send( &p_i->event_manager, &event );
632 int input_item_DelInfo( input_item_t *p_i,
634 const char *psz_name )
636 vlc_mutex_lock( &p_i->lock );
638 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
641 vlc_mutex_unlock( &p_i->lock );
647 /* Remove a specific info */
648 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
651 vlc_mutex_unlock( &p_i->lock );
657 /* Remove the complete categorie */
658 info_category_Delete( p_cat );
659 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
661 vlc_mutex_unlock( &p_i->lock );
665 event.type = vlc_InputItemInfoChanged;
666 vlc_event_send( &p_i->event_manager, &event );
670 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
672 vlc_mutex_lock( &p_item->lock );
674 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
677 info_category_Delete( p_old );
678 p_item->pp_categories[i_cat] = p_cat;
682 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
685 vlc_mutex_unlock( &p_item->lock );
689 event.type = vlc_InputItemInfoChanged;
690 vlc_event_send( &p_item->event_manager, &event );
692 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
694 vlc_mutex_lock( &p_item->lock );
695 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
698 for( int i = 0; i < p_cat->i_infos; i++ )
699 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
700 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
701 info_category_Delete( p_cat );
705 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
708 vlc_mutex_unlock( &p_item->lock );
712 event.type = vlc_InputItemInfoChanged;
713 vlc_event_send( &p_item->event_manager, &event );
717 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
719 vlc_mutex_lock( &p_item->lock );
722 vlc_epg_t *p_epg = NULL;
723 for( int i = 0; i < p_item->i_epg; i++ )
725 vlc_epg_t *p_tmp = p_item->pp_epg[i];
727 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
729 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
739 p_epg = vlc_epg_New( p_update->psz_name );
741 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
744 vlc_epg_Merge( p_epg, p_update );
746 vlc_mutex_unlock( &p_item->lock );
753 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
756 input_item_DelInfo( p_item, psz_epg, NULL );
758 vlc_mutex_lock( &p_item->lock );
759 for( int i = 0; i < p_epg->i_event; i++ )
761 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
762 time_t t_start = (time_t)p_evt->i_start;
766 localtime_r( &t_start, &tm_start );
768 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
769 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
770 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
771 if( p_evt->psz_short_description || p_evt->psz_description )
772 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
774 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
775 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
776 p_evt->psz_description ? p_evt->psz_description : "" );
778 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
780 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
782 vlc_mutex_unlock( &p_item->lock );
787 if( p_epg->i_event > 0 )
789 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
790 vlc_event_send( &p_item->event_manager, &event );
794 void input_item_SetEpgOffline( input_item_t *p_item )
796 vlc_mutex_lock( &p_item->lock );
797 for( int i = 0; i < p_item->i_epg; i++ )
798 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
799 vlc_mutex_unlock( &p_item->lock );
802 vlc_mutex_lock( &p_item->lock );
803 const int i_epg_info = p_item->i_epg;
806 char *ppsz_epg_info[i_epg_info];
807 for( int i = 0; i < p_item->i_epg; i++ )
809 const vlc_epg_t *p_epg = p_item->pp_epg[i];
810 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
811 ppsz_epg_info[i] = NULL;
813 vlc_mutex_unlock( &p_item->lock );
815 for( int i = 0; i < i_epg_info; i++ )
817 if( !ppsz_epg_info[i] )
819 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
820 free( ppsz_epg_info[i] );
824 vlc_mutex_unlock( &p_item->lock );
827 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
828 vlc_event_send( &p_item->event_manager, &event );
831 input_item_t *input_item_NewExt( const char *psz_uri,
832 const char *psz_name,
834 const char *const *ppsz_options,
835 unsigned i_option_flags,
838 return input_item_NewWithType( psz_uri, psz_name,
839 i_options, ppsz_options, i_option_flags,
840 i_duration, ITEM_TYPE_UNKNOWN );
845 input_item_NewWithTypeExt( const char *psz_uri, const char *psz_name,
846 int i_options, const char *const *ppsz_options,
847 unsigned flags, mtime_t duration, int type,
850 static atomic_uint last_input_id = ATOMIC_VAR_INIT(0);
852 input_item_owner_t *owner = calloc( 1, sizeof( *owner ) );
853 if( unlikely(owner == NULL) )
856 atomic_init( &owner->refs, 1 );
858 input_item_t *p_input = &owner->item;
859 vlc_event_manager_t * p_em = &p_input->event_manager;
861 p_input->i_id = atomic_fetch_add(&last_input_id, 1);
862 vlc_mutex_init( &p_input->lock );
864 p_input->psz_name = NULL;
866 input_item_SetName( p_input, psz_name );
868 p_input->psz_uri = NULL;
870 input_item_SetURI( p_input, psz_uri );
872 p_input->i_type = ITEM_TYPE_UNKNOWN;
874 TAB_INIT( p_input->i_options, p_input->ppsz_options );
875 p_input->optflagc = 0;
876 p_input->optflagv = NULL;
877 for( int i = 0; i < i_options; i++ )
878 input_item_AddOption( p_input, ppsz_options[i], flags );
880 p_input->i_duration = duration;
881 TAB_INIT( p_input->i_categories, p_input->pp_categories );
882 TAB_INIT( p_input->i_es, p_input->es );
883 p_input->p_stats = NULL;
884 p_input->p_meta = NULL;
885 TAB_INIT( p_input->i_epg, p_input->pp_epg );
887 vlc_event_manager_init( p_em, p_input );
888 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
889 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
890 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
891 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
892 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
893 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
894 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
895 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
896 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparseEnded );
898 if( type != ITEM_TYPE_UNKNOWN )
899 p_input->i_type = type;
900 p_input->b_error_when_reading = false;
903 p_input->b_net = !!i_net;
904 else if( p_input->i_type == ITEM_TYPE_STREAM )
905 p_input->b_net = true;
910 input_item_NewWithType( const char *psz_uri, const char *psz_name,
911 int i_options, const char *const *ppsz_options,
912 unsigned flags, mtime_t duration, int type )
914 return input_item_NewWithTypeExt( psz_uri, psz_name, i_options,
915 ppsz_options, flags, duration, type,
919 input_item_t *input_item_Copy( input_item_t *p_input )
921 vlc_mutex_lock( &p_input->lock );
923 input_item_t *p_new_input =
924 input_item_NewWithType( p_input->psz_uri, p_input->psz_name,
925 0, NULL, 0, p_input->i_duration,
930 for( int i = 0 ; i< p_input->i_options; i++ )
932 input_item_AddOption( p_new_input,
933 p_input->ppsz_options[i],
934 p_input->optflagv[i] );
937 if( p_input->p_meta )
939 p_new_input->p_meta = vlc_meta_New();
940 vlc_meta_Merge( p_new_input->p_meta, p_input->p_meta );
944 vlc_mutex_unlock( &p_input->lock );
949 struct item_type_entry
951 const char psz_scheme[7];
956 static int typecmp( const void *key, const void *entry )
958 const struct item_type_entry *type = entry;
959 const char *uri = key, *scheme = type->psz_scheme;
961 return strncmp( uri, scheme, strlen( scheme ) );
964 /* Guess the type of the item using the beginning of the mrl */
965 static int GuessType( const input_item_t *p_item, bool *p_net )
967 static const struct item_type_entry tab[] =
968 { /* /!\ Alphabetical order /!\ */
969 /* Short match work, not just exact match */
970 { "alsa", ITEM_TYPE_CARD, false },
971 { "atsc", ITEM_TYPE_CARD, false },
972 { "bd", ITEM_TYPE_DISC, false },
973 { "cable", ITEM_TYPE_CARD, false },
974 { "cdda", ITEM_TYPE_CDDA, false },
975 { "cqam", ITEM_TYPE_CARD, false },
976 { "dc1394", ITEM_TYPE_CARD, false },
977 { "dccp", ITEM_TYPE_STREAM, true },
978 { "deckli", ITEM_TYPE_CARD, false }, /* decklink */
979 { "dir", ITEM_TYPE_DIRECTORY, false },
980 { "dshow", ITEM_TYPE_CARD, false },
981 { "dv", ITEM_TYPE_CARD, false },
982 { "dvb", ITEM_TYPE_CARD, false },
983 { "dvd", ITEM_TYPE_DISC, false },
984 { "dtv", ITEM_TYPE_CARD, false },
985 { "eyetv", ITEM_TYPE_CARD, false },
986 { "fd", ITEM_TYPE_UNKNOWN, false },
987 { "ftp", ITEM_TYPE_FILE, true },
988 { "http", ITEM_TYPE_FILE, true },
989 { "icyx", ITEM_TYPE_STREAM, true },
990 { "imem", ITEM_TYPE_UNKNOWN, false },
991 { "itpc", ITEM_TYPE_PLAYLIST, true },
992 { "jack", ITEM_TYPE_CARD, false },
993 { "linsys", ITEM_TYPE_CARD, false },
994 { "live", ITEM_TYPE_STREAM, true }, /* livedotcom */
995 { "mms", ITEM_TYPE_STREAM, true },
996 { "mtp", ITEM_TYPE_DISC, false },
997 { "ofdm", ITEM_TYPE_CARD, false },
998 { "oss", ITEM_TYPE_CARD, false },
999 { "pnm", ITEM_TYPE_STREAM, true },
1000 { "qam", ITEM_TYPE_CARD, false },
1001 { "qpsk", ITEM_TYPE_CARD, false },
1002 { "qtcapt", ITEM_TYPE_CARD, false }, /* qtcapture */
1003 { "raw139", ITEM_TYPE_CARD, false }, /* raw1394 */
1004 { "rt", ITEM_TYPE_STREAM, true }, /* rtp, rtsp, rtmp */
1005 { "satell", ITEM_TYPE_CARD, false }, /* sattelite */
1006 { "screen", ITEM_TYPE_CARD, false },
1007 { "sdp", ITEM_TYPE_STREAM, true },
1008 { "sftp", ITEM_TYPE_FILE, true },
1009 { "shm", ITEM_TYPE_CARD, false },
1010 { "smb", ITEM_TYPE_FILE, true },
1011 { "svcd", ITEM_TYPE_DISC, false },
1012 { "tcp", ITEM_TYPE_STREAM, true },
1013 { "terres", ITEM_TYPE_CARD, false }, /* terrestrial */
1014 { "udp", ITEM_TYPE_STREAM, true }, /* udplite too */
1015 { "unsv", ITEM_TYPE_STREAM, true },
1016 { "usdigi", ITEM_TYPE_CARD, false }, /* usdigital */
1017 { "v4l", ITEM_TYPE_CARD, false },
1018 { "vcd", ITEM_TYPE_DISC, false },
1019 { "window", ITEM_TYPE_CARD, false },
1021 const struct item_type_entry *e;
1023 if( !strstr( p_item->psz_uri, "://" ) )
1024 return ITEM_TYPE_FILE;
1026 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
1027 sizeof( tab[0] ), typecmp );
1035 return ITEM_TYPE_FILE;
1039 input_item_node_t *input_item_node_Create( input_item_t *p_input )
1041 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
1047 p_node->p_item = p_input;
1048 vlc_gc_incref( p_input );
1050 p_node->p_parent = NULL;
1051 p_node->i_children = 0;
1052 p_node->pp_children = NULL;
1057 static void RecursiveNodeDelete( input_item_node_t *p_node )
1059 for( int i = 0; i < p_node->i_children; i++ )
1060 RecursiveNodeDelete( p_node->pp_children[i] );
1062 vlc_gc_decref( p_node->p_item );
1063 free( p_node->pp_children );
1067 void input_item_node_Delete( input_item_node_t *p_node )
1069 if( p_node->p_parent )
1070 for( int i = 0; i < p_node->p_parent->i_children; i++ )
1071 if( p_node->p_parent->pp_children[i] == p_node )
1073 REMOVE_ELEM( p_node->p_parent->pp_children,
1074 p_node->p_parent->i_children,
1079 RecursiveNodeDelete( p_node );
1082 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1084 int i_preparse_depth;
1085 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1086 if( !p_new_child ) return NULL;
1088 vlc_mutex_lock( &p_node->p_item->lock );
1089 vlc_mutex_lock( &p_item->lock );
1090 i_preparse_depth = p_node->p_item->i_preparse_depth;
1091 p_item->i_preparse_depth = i_preparse_depth > 0 ?
1092 i_preparse_depth -1 :
1094 vlc_mutex_unlock( &p_item->lock );
1095 vlc_mutex_unlock( &p_node->p_item->lock );
1097 input_item_node_AppendNode( p_node, p_new_child );
1101 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1103 assert( p_parent && p_child && p_child->p_parent == NULL );
1104 INSERT_ELEM( p_parent->pp_children,
1105 p_parent->i_children,
1106 p_parent->i_children,
1108 p_child->p_parent = p_parent;
1111 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1113 post_subitems( p_root );
1116 event.type = vlc_InputItemSubItemTreeAdded;
1117 event.u.input_item_subitem_tree_added.p_root = p_root;
1118 vlc_event_send( &p_root->p_item->event_manager, &event );
1120 input_item_node_Delete( p_root );
1123 /* Called by es_out when a new Elementary Stream is added or updated. */
1124 void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
1127 es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
1131 es_format_Copy(fmt_copy, fmt);
1132 /* XXX: we could free p_extra to save memory, we will likely not need
1133 * the decoder specific data */
1135 vlc_mutex_lock( &item->lock );
1137 for( i = 0; i < item->i_es; i++ )
1139 if (item->es[i]->i_id != fmt->i_id)
1142 /* We've found the right ES, replace it */
1143 es_format_Clean(item->es[i]);
1145 item->es[i] = fmt_copy;
1146 vlc_mutex_unlock( &item->lock );
1150 /* ES not found, insert it */
1151 TAB_APPEND(item->i_es, item->es, fmt_copy);
1152 vlc_mutex_unlock( &item->lock );