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 );
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 );
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 input_item_t *input_item_Hold( input_item_t *p_item )
428 input_item_owner_t *owner = item_owner(p_item);
430 atomic_fetch_add( &owner->refs, 1 );
434 void input_item_Release( input_item_t *p_item )
436 input_item_owner_t *owner = item_owner(p_item);
438 if( atomic_fetch_sub(&owner->refs, 1) != 1 )
441 vlc_event_manager_fini( &p_item->event_manager );
443 free( p_item->psz_name );
444 free( p_item->psz_uri );
445 if( p_item->p_stats != NULL )
447 vlc_mutex_destroy( &p_item->p_stats->lock );
448 free( p_item->p_stats );
451 if( p_item->p_meta != NULL )
452 vlc_meta_Delete( p_item->p_meta );
454 for( int i = 0; i < p_item->i_options; i++ )
455 free( p_item->ppsz_options[i] );
456 TAB_CLEAN( p_item->i_options, p_item->ppsz_options );
457 free( p_item->optflagv );
459 for( int i = 0; i < p_item->i_es; i++ )
461 es_format_Clean( p_item->es[i] );
462 free( p_item->es[i] );
464 TAB_CLEAN( p_item->i_es, p_item->es );
466 for( int i = 0; i < p_item->i_epg; i++ )
467 vlc_epg_Delete( p_item->pp_epg[i] );
468 TAB_CLEAN( p_item->i_epg, p_item->pp_epg );
470 for( int i = 0; i < p_item->i_categories; i++ )
471 info_category_Delete( p_item->pp_categories[i] );
472 TAB_CLEAN( p_item->i_categories, p_item->pp_categories );
474 vlc_mutex_destroy( &p_item->lock );
478 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
481 int err = VLC_SUCCESS;
483 if( psz_option == NULL )
486 vlc_mutex_lock( &p_input->lock );
487 if (flags & VLC_INPUT_OPTION_UNIQUE)
489 for (int i = 0 ; i < p_input->i_options; i++)
490 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
494 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
500 p_input->optflagv = flagv;
501 flagv[p_input->optflagc++] = flags;
503 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
504 p_input->i_options, strdup( psz_option ) );
506 vlc_mutex_unlock( &p_input->lock );
510 static info_category_t *InputItemFindCat( input_item_t *p_item,
511 int *pi_index, const char *psz_cat )
513 vlc_assert_locked( &p_item->lock );
514 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
516 info_category_t *p_cat = p_item->pp_categories[i];
518 if( !strcmp( p_cat->psz_name, psz_cat ) )
529 * Get a info item from a given category in a given input item.
531 * \param p_i The input item to get info from
532 * \param psz_cat String representing the category for the info
533 * \param psz_name String representing the name of the desired info
534 * \return A pointer to the string with the given info if found, or an
535 * empty string otherwise. The caller should free the returned
538 char *input_item_GetInfo( input_item_t *p_i,
540 const char *psz_name )
542 vlc_mutex_lock( &p_i->lock );
544 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
547 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
548 if( p_info && p_info->psz_value )
550 char *psz_ret = strdup( p_info->psz_value );
551 vlc_mutex_unlock( &p_i->lock );
555 vlc_mutex_unlock( &p_i->lock );
559 static int InputItemVaAddInfo( input_item_t *p_i,
561 const char *psz_name,
562 const char *psz_format, va_list args )
564 vlc_assert_locked( &p_i->lock );
566 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
569 p_cat = info_category_New( psz_cat );
572 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
575 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
576 if( !p_info || !p_info->psz_value )
581 static int InputItemAddInfo( input_item_t *p_i,
583 const char *psz_name,
584 const char *psz_format, ... )
588 va_start( args, psz_format );
589 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
595 int input_item_AddInfo( input_item_t *p_i,
597 const char *psz_name,
598 const char *psz_format, ... )
602 vlc_mutex_lock( &p_i->lock );
604 va_start( args, psz_format );
605 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
608 vlc_mutex_unlock( &p_i->lock );
615 event.type = vlc_InputItemInfoChanged;
616 vlc_event_send( &p_i->event_manager, &event );
621 int input_item_DelInfo( input_item_t *p_i,
623 const char *psz_name )
625 vlc_mutex_lock( &p_i->lock );
627 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
630 vlc_mutex_unlock( &p_i->lock );
636 /* Remove a specific info */
637 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
640 vlc_mutex_unlock( &p_i->lock );
646 /* Remove the complete categorie */
647 info_category_Delete( p_cat );
648 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
650 vlc_mutex_unlock( &p_i->lock );
654 event.type = vlc_InputItemInfoChanged;
655 vlc_event_send( &p_i->event_manager, &event );
659 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
661 vlc_mutex_lock( &p_item->lock );
663 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
666 info_category_Delete( p_old );
667 p_item->pp_categories[i_cat] = p_cat;
671 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
674 vlc_mutex_unlock( &p_item->lock );
678 event.type = vlc_InputItemInfoChanged;
679 vlc_event_send( &p_item->event_manager, &event );
681 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
683 vlc_mutex_lock( &p_item->lock );
684 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
687 for( int i = 0; i < p_cat->i_infos; i++ )
688 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
689 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
690 info_category_Delete( p_cat );
694 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
697 vlc_mutex_unlock( &p_item->lock );
701 event.type = vlc_InputItemInfoChanged;
702 vlc_event_send( &p_item->event_manager, &event );
706 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
708 vlc_mutex_lock( &p_item->lock );
711 vlc_epg_t *p_epg = NULL;
712 for( int i = 0; i < p_item->i_epg; i++ )
714 vlc_epg_t *p_tmp = p_item->pp_epg[i];
716 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
718 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
728 p_epg = vlc_epg_New( p_update->psz_name );
730 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
733 vlc_epg_Merge( p_epg, p_update );
735 vlc_mutex_unlock( &p_item->lock );
742 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
745 input_item_DelInfo( p_item, psz_epg, NULL );
747 vlc_mutex_lock( &p_item->lock );
748 for( int i = 0; i < p_epg->i_event; i++ )
750 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
751 time_t t_start = (time_t)p_evt->i_start;
755 localtime_r( &t_start, &tm_start );
757 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
758 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
759 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
760 if( p_evt->psz_short_description || p_evt->psz_description )
761 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
763 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
764 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
765 p_evt->psz_description ? p_evt->psz_description : "" );
767 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
769 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
771 vlc_mutex_unlock( &p_item->lock );
776 if( p_epg->i_event > 0 )
778 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
779 vlc_event_send( &p_item->event_manager, &event );
783 void input_item_SetEpgOffline( input_item_t *p_item )
785 vlc_mutex_lock( &p_item->lock );
786 for( int i = 0; i < p_item->i_epg; i++ )
787 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
788 vlc_mutex_unlock( &p_item->lock );
791 vlc_mutex_lock( &p_item->lock );
792 const int i_epg_info = p_item->i_epg;
795 char *ppsz_epg_info[i_epg_info];
796 for( int i = 0; i < p_item->i_epg; i++ )
798 const vlc_epg_t *p_epg = p_item->pp_epg[i];
799 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
800 ppsz_epg_info[i] = NULL;
802 vlc_mutex_unlock( &p_item->lock );
804 for( int i = 0; i < i_epg_info; i++ )
806 if( !ppsz_epg_info[i] )
808 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
809 free( ppsz_epg_info[i] );
813 vlc_mutex_unlock( &p_item->lock );
816 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
817 vlc_event_send( &p_item->event_manager, &event );
820 input_item_t *input_item_NewExt( 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( psz_uri, psz_name,
828 i_options, ppsz_options, i_option_flags,
829 i_duration, ITEM_TYPE_UNKNOWN );
834 input_item_NewWithType( const char *psz_uri, const char *psz_name,
835 int i_options, const char *const *ppsz_options,
836 unsigned flags, mtime_t duration, int type )
838 static atomic_uint last_input_id = ATOMIC_VAR_INIT(0);
840 input_item_owner_t *owner = calloc( 1, sizeof( *owner ) );
841 if( unlikely(owner == NULL) )
844 atomic_init( &owner->refs, 1 );
846 input_item_t *p_input = &owner->item;
847 vlc_event_manager_t * p_em = &p_input->event_manager;
849 p_input->i_id = atomic_fetch_add(&last_input_id, 1);
850 vlc_mutex_init( &p_input->lock );
852 p_input->psz_name = NULL;
854 input_item_SetName( p_input, psz_name );
856 p_input->psz_uri = NULL;
858 input_item_SetURI( p_input, psz_uri );
860 p_input->i_type = ITEM_TYPE_UNKNOWN;
862 TAB_INIT( p_input->i_options, p_input->ppsz_options );
863 p_input->optflagc = 0;
864 p_input->optflagv = NULL;
865 for( int i = 0; i < i_options; i++ )
866 input_item_AddOption( p_input, ppsz_options[i], flags );
868 p_input->i_duration = duration;
869 TAB_INIT( p_input->i_categories, p_input->pp_categories );
870 TAB_INIT( p_input->i_es, p_input->es );
871 p_input->p_stats = NULL;
872 p_input->p_meta = NULL;
873 TAB_INIT( p_input->i_epg, p_input->pp_epg );
875 vlc_event_manager_init( p_em, p_input );
876 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
877 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
878 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
879 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
880 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
881 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
882 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
883 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
884 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparseEnded );
886 if( type != ITEM_TYPE_UNKNOWN )
887 p_input->i_type = type;
888 p_input->b_error_when_reading = false;
892 input_item_t *input_item_Copy( input_item_t *p_input )
894 vlc_mutex_lock( &p_input->lock );
896 input_item_t *p_new_input =
897 input_item_NewWithType( p_input->psz_uri, p_input->psz_name,
898 0, NULL, 0, p_input->i_duration,
903 for( int i = 0 ; i< p_input->i_options; i++ )
905 input_item_AddOption( p_new_input,
906 p_input->ppsz_options[i],
907 p_input->optflagv[i] );
910 if( p_input->p_meta )
912 p_new_input->p_meta = vlc_meta_New();
913 vlc_meta_Merge( p_new_input->p_meta, p_input->p_meta );
917 vlc_mutex_unlock( &p_input->lock );
922 struct item_type_entry
924 const char psz_scheme[7];
928 static int typecmp( const void *key, const void *entry )
930 const struct item_type_entry *type = entry;
931 const char *uri = key, *scheme = type->psz_scheme;
933 return strncmp( uri, scheme, strlen( scheme ) );
936 /* Guess the type of the item using the beginning of the mrl */
937 static int GuessType( const input_item_t *p_item )
939 static const struct item_type_entry tab[] =
940 { /* /!\ Alphabetical order /!\ */
941 /* Short match work, not just exact match */
942 { "alsa", ITEM_TYPE_CARD },
943 { "atsc", ITEM_TYPE_CARD },
944 { "bd", ITEM_TYPE_DISC },
945 { "cable", ITEM_TYPE_CARD },
946 { "cdda", ITEM_TYPE_CDDA },
947 { "cqam", ITEM_TYPE_CARD },
948 { "dc1394", ITEM_TYPE_CARD },
949 { "dccp", ITEM_TYPE_STREAM },
950 { "deckli", ITEM_TYPE_CARD }, /* decklink */
951 { "dir", ITEM_TYPE_DIRECTORY },
952 { "dshow", ITEM_TYPE_CARD },
953 { "dv", ITEM_TYPE_CARD },
954 { "dvb", ITEM_TYPE_CARD },
955 { "dvd", ITEM_TYPE_DISC },
956 { "dtv", ITEM_TYPE_CARD },
957 { "eyetv", ITEM_TYPE_CARD },
958 { "fd", ITEM_TYPE_UNKNOWN },
959 { "ftp", ITEM_TYPE_STREAM },
960 { "http", ITEM_TYPE_STREAM },
961 { "icyx", ITEM_TYPE_STREAM },
962 { "imem", ITEM_TYPE_UNKNOWN },
963 { "itpc", ITEM_TYPE_STREAM },
964 { "jack", ITEM_TYPE_CARD },
965 { "linsys", ITEM_TYPE_CARD },
966 { "live", ITEM_TYPE_STREAM }, /* livedotcom */
967 { "mms", ITEM_TYPE_STREAM },
968 { "mtp", ITEM_TYPE_DISC },
969 { "ofdm", ITEM_TYPE_CARD },
970 { "oss", ITEM_TYPE_CARD },
971 { "pnm", ITEM_TYPE_STREAM },
972 { "qam", ITEM_TYPE_CARD },
973 { "qpsk", ITEM_TYPE_CARD },
974 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
975 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
976 { "rt", ITEM_TYPE_STREAM }, /* rtp, rtsp, rtmp */
977 { "satell", ITEM_TYPE_CARD }, /* sattelite */
978 { "screen", ITEM_TYPE_CARD },
979 { "sdp", ITEM_TYPE_STREAM },
980 { "sftp", ITEM_TYPE_STREAM },
981 { "shm", ITEM_TYPE_CARD },
982 { "smb", ITEM_TYPE_STREAM },
983 { "svcd", ITEM_TYPE_DISC },
984 { "tcp", ITEM_TYPE_STREAM },
985 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
986 { "udp", ITEM_TYPE_STREAM }, /* udplite too */
987 { "unsv", ITEM_TYPE_STREAM },
988 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
989 { "v4l", ITEM_TYPE_CARD },
990 { "vcd", ITEM_TYPE_DISC },
991 { "window", ITEM_TYPE_CARD },
993 const struct item_type_entry *e;
995 if( !strstr( p_item->psz_uri, "://" ) )
996 return ITEM_TYPE_FILE;
998 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
999 sizeof( tab[0] ), typecmp );
1000 return e ? e->i_type : ITEM_TYPE_FILE;
1003 input_item_node_t *input_item_node_Create( input_item_t *p_input )
1005 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
1011 p_node->p_item = p_input;
1012 vlc_gc_incref( p_input );
1014 p_node->p_parent = NULL;
1015 p_node->i_children = 0;
1016 p_node->pp_children = NULL;
1021 static void RecursiveNodeDelete( input_item_node_t *p_node )
1023 for( int i = 0; i < p_node->i_children; i++ )
1024 RecursiveNodeDelete( p_node->pp_children[i] );
1026 vlc_gc_decref( p_node->p_item );
1027 free( p_node->pp_children );
1031 void input_item_node_Delete( input_item_node_t *p_node )
1033 if( p_node->p_parent )
1034 for( int i = 0; i < p_node->p_parent->i_children; i++ )
1035 if( p_node->p_parent->pp_children[i] == p_node )
1037 REMOVE_ELEM( p_node->p_parent->pp_children,
1038 p_node->p_parent->i_children,
1043 RecursiveNodeDelete( p_node );
1046 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1048 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1049 if( !p_new_child ) return NULL;
1050 input_item_node_AppendNode( p_node, p_new_child );
1054 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1056 assert( p_parent && p_child && p_child->p_parent == NULL );
1057 INSERT_ELEM( p_parent->pp_children,
1058 p_parent->i_children,
1059 p_parent->i_children,
1061 p_child->p_parent = p_parent;
1064 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1066 post_subitems( p_root );
1069 event.type = vlc_InputItemSubItemTreeAdded;
1070 event.u.input_item_subitem_tree_added.p_root = p_root;
1071 vlc_event_send( &p_root->p_item->event_manager, &event );
1073 input_item_node_Delete( p_root );
1076 /* Called by es_out when a new Elementary Stream is added or updated. */
1077 void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
1080 es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
1084 es_format_Copy(fmt_copy, fmt);
1085 /* XXX: we could free p_extra to save memory, we will likely not need
1086 * the decoder specific data */
1088 vlc_mutex_lock( &item->lock );
1090 for( i = 0; i < item->i_es; i++ )
1092 if (item->es[i]->i_id != fmt->i_id)
1095 /* We've found the right ES, replace it */
1096 es_format_Clean(item->es[i]);
1098 item->es[i] = fmt_copy;
1099 vlc_mutex_unlock( &item->lock );
1103 /* ES not found, insert it */
1104 TAB_APPEND(item->i_es, item->es, fmt_copy);
1105 vlc_mutex_unlock( &item->lock );