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_SetPreparsed( input_item_t *p_i, bool b_preparsed )
62 bool b_send_event = false;
64 vlc_mutex_lock( &p_i->lock );
67 p_i->p_meta = vlc_meta_New();
69 int status = vlc_meta_GetStatus(p_i->p_meta);
72 new_status = status | ITEM_PREPARSED;
74 new_status = status & ~ITEM_PREPARSED;
75 if( status != new_status )
77 vlc_meta_SetStatus(p_i->p_meta, new_status);
81 vlc_mutex_unlock( &p_i->lock );
86 event.type = vlc_InputItemPreparsedChanged;
87 event.u.input_item_preparsed_changed.new_status = new_status;
88 vlc_event_send( &p_i->event_manager, &event );
92 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
94 vlc_mutex_lock( &p_i->lock );
97 p_i->p_meta = vlc_meta_New();
99 int status = vlc_meta_GetStatus(p_i->p_meta);
102 status |= ITEM_ART_NOTFOUND;
104 status &= ~ITEM_ART_NOTFOUND;
106 vlc_meta_SetStatus(p_i->p_meta, status);
108 vlc_mutex_unlock( &p_i->lock );
111 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
113 vlc_mutex_lock( &p_i->lock );
116 p_i->p_meta = vlc_meta_New();
118 int status = vlc_meta_GetStatus(p_i->p_meta);
121 status |= ITEM_ART_FETCHED;
123 status &= ~ITEM_ART_FETCHED;
125 vlc_meta_SetStatus(p_i->p_meta, status);
127 vlc_mutex_unlock( &p_i->lock );
130 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
134 vlc_mutex_lock( &p_i->lock );
136 p_i->p_meta = vlc_meta_New();
137 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
138 vlc_mutex_unlock( &p_i->lock );
140 /* Notify interested third parties */
141 event.type = vlc_InputItemMetaChanged;
142 event.u.input_item_meta_changed.meta_type = meta_type;
143 vlc_event_send( &p_i->event_manager, &event );
146 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
147 * consistent with (nearly?) all or copy funcs */
148 void input_item_CopyOptions( input_item_t *p_parent,
149 input_item_t *p_child )
151 vlc_mutex_lock( &p_parent->lock );
153 for( int i = 0 ; i< p_parent->i_options; i++ )
155 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
158 input_item_AddOption( p_child,
159 p_parent->ppsz_options[i],
160 p_parent->optflagv[i] );
163 vlc_mutex_unlock( &p_parent->lock );
166 static void post_subitems( input_item_node_t *p_node )
168 for( int i = 0; i < p_node->i_children; i++ )
171 event.type = vlc_InputItemSubItemAdded;
172 event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
173 vlc_event_send( &p_node->p_item->event_manager, &event );
175 post_subitems( p_node->pp_children[i] );
179 /* This won't hold the item, but can tell to interested third parties
180 * Like the playlist, that there is a new sub item. With this design
181 * It is not the input item's responsability to keep all the ref of
182 * the input item children. */
183 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
185 input_item_node_t *p_node = input_item_node_Create( p_parent );
186 input_item_node_AppendItem( p_node, p_child );
187 input_item_node_PostAndDelete( p_node );
190 bool input_item_HasErrorWhenReading( input_item_t *p_item )
192 vlc_mutex_lock( &p_item->lock );
194 bool b_error = p_item->b_error_when_reading;
196 vlc_mutex_unlock( &p_item->lock );
201 bool input_item_MetaMatch( input_item_t *p_i,
202 vlc_meta_type_t meta_type, const char *psz )
204 vlc_mutex_lock( &p_i->lock );
208 vlc_mutex_unlock( &p_i->lock );
211 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
212 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
214 vlc_mutex_unlock( &p_i->lock );
219 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
221 vlc_mutex_lock( &p_i->lock );
225 vlc_mutex_unlock( &p_i->lock );
230 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
231 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
233 vlc_mutex_unlock( &p_i->lock );
237 /* Get the title of a given item or fallback to the name if the title is empty */
238 char *input_item_GetTitleFbName( input_item_t *p_item )
241 vlc_mutex_lock( &p_item->lock );
243 if( !p_item->p_meta )
245 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
246 vlc_mutex_unlock( &p_item->lock );
250 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
251 if( !EMPTY_STR( psz_title ) )
252 psz_ret = strdup( psz_title );
254 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
256 vlc_mutex_unlock( &p_item->lock );
260 char *input_item_GetName( input_item_t *p_item )
262 vlc_mutex_lock( &p_item->lock );
264 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
266 vlc_mutex_unlock( &p_item->lock );
269 void input_item_SetName( input_item_t *p_item, const char *psz_name )
271 vlc_mutex_lock( &p_item->lock );
273 free( p_item->psz_name );
274 p_item->psz_name = strdup( psz_name );
276 vlc_mutex_unlock( &p_item->lock );
279 char *input_item_GetURI( input_item_t *p_i )
281 vlc_mutex_lock( &p_i->lock );
283 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
285 vlc_mutex_unlock( &p_i->lock );
289 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
293 if( !strstr( psz_uri, "://" )
294 || strchr( psz_uri, ' ' ) || strchr( psz_uri, '"' ) )
295 fprintf( stderr, "Warning: %s(\"%s\"): file path instead of URL.\n",
298 vlc_mutex_lock( &p_i->lock );
299 free( p_i->psz_uri );
300 p_i->psz_uri = strdup( psz_uri );
302 p_i->i_type = GuessType( p_i );
307 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
309 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
311 if( psz_filename && *psz_filename == '/' )
313 if( psz_filename && *psz_filename )
314 p_i->psz_name = strdup( psz_filename );
316 /* Make the name more readable */
319 decode_URI( p_i->psz_name );
320 EnsureUTF8( p_i->psz_name );
324 { /* Strip login and password from title */
328 vlc_UrlParse( &url, psz_uri, 0 );
329 if( url.psz_protocol )
332 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
333 url.psz_host, url.i_port,
334 url.psz_path ? url.psz_path : "" );
336 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
337 url.psz_host ? url.psz_host : "",
338 url.psz_path ? url.psz_path : "" );
343 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
344 url.psz_path ? url.psz_path : "" );
346 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
347 url.psz_path ? url.psz_path : "" );
349 vlc_UrlClean( &url );
351 p_i->psz_name=NULL; /* recover from undefined value */
354 vlc_mutex_unlock( &p_i->lock );
357 mtime_t input_item_GetDuration( input_item_t *p_i )
359 vlc_mutex_lock( &p_i->lock );
361 mtime_t i_duration = p_i->i_duration;
363 vlc_mutex_unlock( &p_i->lock );
367 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
369 bool b_send_event = false;
371 vlc_mutex_lock( &p_i->lock );
372 if( p_i->i_duration != i_duration )
374 p_i->i_duration = i_duration;
377 vlc_mutex_unlock( &p_i->lock );
383 event.type = vlc_InputItemDurationChanged;
384 event.u.input_item_duration_changed.new_duration = i_duration;
385 vlc_event_send( &p_i->event_manager, &event );
390 bool input_item_IsPreparsed( input_item_t *p_item )
392 vlc_mutex_lock( &p_item->lock );
393 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
394 vlc_mutex_unlock( &p_item->lock );
399 bool input_item_IsArtFetched( input_item_t *p_item )
401 vlc_mutex_lock( &p_item->lock );
402 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
403 vlc_mutex_unlock( &p_item->lock );
408 input_item_t *input_item_Hold( input_item_t *p_item )
410 input_item_owner_t *owner = item_owner(p_item);
412 atomic_fetch_add( &owner->refs, 1 );
416 void input_item_Release( input_item_t *p_item )
418 input_item_owner_t *owner = item_owner(p_item);
420 if( atomic_fetch_sub(&owner->refs, 1) != 1 )
423 vlc_event_manager_fini( &p_item->event_manager );
425 free( p_item->psz_name );
426 free( p_item->psz_uri );
427 if( p_item->p_stats != NULL )
429 vlc_mutex_destroy( &p_item->p_stats->lock );
430 free( p_item->p_stats );
433 if( p_item->p_meta != NULL )
434 vlc_meta_Delete( p_item->p_meta );
436 for( int i = 0; i < p_item->i_options; i++ )
437 free( p_item->ppsz_options[i] );
438 TAB_CLEAN( p_item->i_options, p_item->ppsz_options );
439 free( p_item->optflagv );
441 for( int i = 0; i < p_item->i_es; i++ )
443 es_format_Clean( p_item->es[i] );
444 free( p_item->es[i] );
446 TAB_CLEAN( p_item->i_es, p_item->es );
448 for( int i = 0; i < p_item->i_epg; i++ )
449 vlc_epg_Delete( p_item->pp_epg[i] );
450 TAB_CLEAN( p_item->i_epg, p_item->pp_epg );
452 for( int i = 0; i < p_item->i_categories; i++ )
453 info_category_Delete( p_item->pp_categories[i] );
454 TAB_CLEAN( p_item->i_categories, p_item->pp_categories );
456 vlc_mutex_destroy( &p_item->lock );
460 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
463 int err = VLC_SUCCESS;
465 if( psz_option == NULL )
468 vlc_mutex_lock( &p_input->lock );
469 if (flags & VLC_INPUT_OPTION_UNIQUE)
471 for (int i = 0 ; i < p_input->i_options; i++)
472 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
476 if (flags & VLC_INPUT_OPTION_REPLACE)
478 /* search for option format "option=value" */
480 const char *psz_found = strchr( psz_option, '=' );
481 if( likely( psz_found ) )
483 for( int i = 0 ; i < p_input->i_options; i++ )
485 if( !strncmp( p_input->ppsz_options[i], psz_option,
486 psz_found - psz_option + 1 /* = */ ) )
488 free( p_input->ppsz_options[i] );
489 p_input->ppsz_options[i] = strdup( psz_option );
498 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
504 p_input->optflagv = flagv;
505 flagv[p_input->optflagc++] = flags;
507 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
508 p_input->i_options, strdup( psz_option ) );
510 vlc_mutex_unlock( &p_input->lock );
514 static info_category_t *InputItemFindCat( input_item_t *p_item,
515 int *pi_index, const char *psz_cat )
517 vlc_assert_locked( &p_item->lock );
518 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
520 info_category_t *p_cat = p_item->pp_categories[i];
522 if( !strcmp( p_cat->psz_name, psz_cat ) )
533 * Get a info item from a given category in a given input item.
535 * \param p_i The input item to get info from
536 * \param psz_cat String representing the category for the info
537 * \param psz_name String representing the name of the desired info
538 * \return A pointer to the string with the given info if found, or an
539 * empty string otherwise. The caller should free the returned
542 char *input_item_GetInfo( input_item_t *p_i,
544 const char *psz_name )
546 vlc_mutex_lock( &p_i->lock );
548 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
551 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
552 if( p_info && p_info->psz_value )
554 char *psz_ret = strdup( p_info->psz_value );
555 vlc_mutex_unlock( &p_i->lock );
559 vlc_mutex_unlock( &p_i->lock );
563 static int InputItemVaAddInfo( input_item_t *p_i,
565 const char *psz_name,
566 const char *psz_format, va_list args )
568 vlc_assert_locked( &p_i->lock );
570 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
573 p_cat = info_category_New( psz_cat );
576 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
579 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
580 if( !p_info || !p_info->psz_value )
585 static int InputItemAddInfo( input_item_t *p_i,
587 const char *psz_name,
588 const char *psz_format, ... )
592 va_start( args, psz_format );
593 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
599 int input_item_AddInfo( input_item_t *p_i,
601 const char *psz_name,
602 const char *psz_format, ... )
606 vlc_mutex_lock( &p_i->lock );
608 va_start( args, psz_format );
609 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
612 vlc_mutex_unlock( &p_i->lock );
619 event.type = vlc_InputItemInfoChanged;
620 vlc_event_send( &p_i->event_manager, &event );
625 int input_item_DelInfo( input_item_t *p_i,
627 const char *psz_name )
629 vlc_mutex_lock( &p_i->lock );
631 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
634 vlc_mutex_unlock( &p_i->lock );
640 /* Remove a specific info */
641 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
644 vlc_mutex_unlock( &p_i->lock );
650 /* Remove the complete categorie */
651 info_category_Delete( p_cat );
652 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
654 vlc_mutex_unlock( &p_i->lock );
658 event.type = vlc_InputItemInfoChanged;
659 vlc_event_send( &p_i->event_manager, &event );
663 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
665 vlc_mutex_lock( &p_item->lock );
667 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
670 info_category_Delete( p_old );
671 p_item->pp_categories[i_cat] = p_cat;
675 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
678 vlc_mutex_unlock( &p_item->lock );
682 event.type = vlc_InputItemInfoChanged;
683 vlc_event_send( &p_item->event_manager, &event );
685 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
687 vlc_mutex_lock( &p_item->lock );
688 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
691 for( int i = 0; i < p_cat->i_infos; i++ )
692 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
693 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
694 info_category_Delete( p_cat );
698 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
701 vlc_mutex_unlock( &p_item->lock );
705 event.type = vlc_InputItemInfoChanged;
706 vlc_event_send( &p_item->event_manager, &event );
710 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
712 vlc_mutex_lock( &p_item->lock );
715 vlc_epg_t *p_epg = NULL;
716 for( int i = 0; i < p_item->i_epg; i++ )
718 vlc_epg_t *p_tmp = p_item->pp_epg[i];
720 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
722 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
732 p_epg = vlc_epg_New( p_update->psz_name );
734 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
737 vlc_epg_Merge( p_epg, p_update );
739 vlc_mutex_unlock( &p_item->lock );
746 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
749 input_item_DelInfo( p_item, psz_epg, NULL );
751 vlc_mutex_lock( &p_item->lock );
752 for( int i = 0; i < p_epg->i_event; i++ )
754 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
755 time_t t_start = (time_t)p_evt->i_start;
759 localtime_r( &t_start, &tm_start );
761 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
762 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
763 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
764 if( p_evt->psz_short_description || p_evt->psz_description )
765 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
767 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
768 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
769 p_evt->psz_description ? p_evt->psz_description : "" );
771 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
773 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
775 vlc_mutex_unlock( &p_item->lock );
780 if( p_epg->i_event > 0 )
782 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
783 vlc_event_send( &p_item->event_manager, &event );
787 void input_item_SetEpgOffline( input_item_t *p_item )
789 vlc_mutex_lock( &p_item->lock );
790 for( int i = 0; i < p_item->i_epg; i++ )
791 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
792 vlc_mutex_unlock( &p_item->lock );
795 vlc_mutex_lock( &p_item->lock );
796 const int i_epg_info = p_item->i_epg;
799 char *ppsz_epg_info[i_epg_info];
800 for( int i = 0; i < p_item->i_epg; i++ )
802 const vlc_epg_t *p_epg = p_item->pp_epg[i];
803 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
804 ppsz_epg_info[i] = NULL;
806 vlc_mutex_unlock( &p_item->lock );
808 for( int i = 0; i < i_epg_info; i++ )
810 if( !ppsz_epg_info[i] )
812 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
813 free( ppsz_epg_info[i] );
817 vlc_mutex_unlock( &p_item->lock );
820 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
821 vlc_event_send( &p_item->event_manager, &event );
824 input_item_t *input_item_NewExt( const char *psz_uri,
825 const char *psz_name,
827 const char *const *ppsz_options,
828 unsigned i_option_flags,
831 return input_item_NewWithType( psz_uri, psz_name,
832 i_options, ppsz_options, i_option_flags,
833 i_duration, ITEM_TYPE_UNKNOWN );
838 input_item_NewWithType( const char *psz_uri, const char *psz_name,
839 int i_options, const char *const *ppsz_options,
840 unsigned flags, mtime_t duration, int type )
842 static atomic_uint last_input_id = ATOMIC_VAR_INIT(0);
844 input_item_owner_t *owner = calloc( 1, sizeof( *owner ) );
845 if( unlikely(owner == NULL) )
848 atomic_init( &owner->refs, 1 );
850 input_item_t *p_input = &owner->item;
851 vlc_event_manager_t * p_em = &p_input->event_manager;
853 p_input->i_id = atomic_fetch_add(&last_input_id, 1);
854 vlc_mutex_init( &p_input->lock );
856 p_input->psz_name = NULL;
858 input_item_SetName( p_input, psz_name );
860 p_input->psz_uri = NULL;
862 input_item_SetURI( p_input, psz_uri );
864 p_input->i_type = ITEM_TYPE_UNKNOWN;
866 TAB_INIT( p_input->i_options, p_input->ppsz_options );
867 p_input->optflagc = 0;
868 p_input->optflagv = NULL;
869 for( int i = 0; i < i_options; i++ )
870 input_item_AddOption( p_input, ppsz_options[i], flags );
872 p_input->i_duration = duration;
873 TAB_INIT( p_input->i_categories, p_input->pp_categories );
874 TAB_INIT( p_input->i_es, p_input->es );
875 p_input->p_stats = NULL;
876 p_input->i_nb_played = 0;
877 p_input->p_meta = NULL;
878 TAB_INIT( p_input->i_epg, p_input->pp_epg );
880 vlc_event_manager_init( p_em, p_input );
881 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
882 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
883 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
884 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
885 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
886 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
887 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
888 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
890 if( type != ITEM_TYPE_UNKNOWN )
891 p_input->i_type = type;
892 p_input->b_fixed_name = false;
893 p_input->b_error_when_reading = false;
897 input_item_t *input_item_Copy( input_item_t *p_input )
899 vlc_mutex_lock( &p_input->lock );
901 input_item_t *p_new_input =
902 input_item_NewWithType( p_input->psz_uri, p_input->psz_name,
903 0, NULL, 0, p_input->i_duration,
908 for( int i = 0 ; i< p_input->i_options; i++ )
910 input_item_AddOption( p_new_input,
911 p_input->ppsz_options[i],
912 p_input->optflagv[i] );
915 if( p_input->p_meta )
917 p_new_input->p_meta = vlc_meta_New();
918 vlc_meta_Merge( p_new_input->p_meta, p_input->p_meta );
922 vlc_mutex_unlock( &p_input->lock );
927 struct item_type_entry
929 const char psz_scheme[7];
933 static int typecmp( const void *key, const void *entry )
935 const struct item_type_entry *type = entry;
936 const char *uri = key, *scheme = type->psz_scheme;
938 return strncmp( uri, scheme, strlen( scheme ) );
941 /* Guess the type of the item using the beginning of the mrl */
942 static int GuessType( const input_item_t *p_item )
944 static const struct item_type_entry tab[] =
945 { /* /!\ Alphabetical order /!\ */
946 /* Short match work, not just exact match */
947 { "alsa", ITEM_TYPE_CARD },
948 { "atsc", ITEM_TYPE_CARD },
949 { "bd", ITEM_TYPE_DISC },
950 { "cable", ITEM_TYPE_CARD },
951 { "cdda", ITEM_TYPE_CDDA },
952 { "cqam", ITEM_TYPE_CARD },
953 { "dc1394", ITEM_TYPE_CARD },
954 { "dccp", ITEM_TYPE_NET },
955 { "deckli", ITEM_TYPE_CARD }, /* decklink */
956 { "dir", ITEM_TYPE_DIRECTORY },
957 { "dshow", ITEM_TYPE_CARD },
958 { "dv", ITEM_TYPE_CARD },
959 { "dvb", ITEM_TYPE_CARD },
960 { "dvd", ITEM_TYPE_DISC },
961 { "dtv", ITEM_TYPE_CARD },
962 { "eyetv", ITEM_TYPE_CARD },
963 { "fd", ITEM_TYPE_UNKNOWN },
964 { "ftp", ITEM_TYPE_NET },
965 { "http", ITEM_TYPE_NET },
966 { "icyx", ITEM_TYPE_NET },
967 { "imem", ITEM_TYPE_UNKNOWN },
968 { "itpc", ITEM_TYPE_NET },
969 { "jack", ITEM_TYPE_CARD },
970 { "linsys", ITEM_TYPE_CARD },
971 { "live", ITEM_TYPE_NET }, /* livedotcom */
972 { "mms", ITEM_TYPE_NET },
973 { "mtp", ITEM_TYPE_DISC },
974 { "ofdm", ITEM_TYPE_CARD },
975 { "oss", ITEM_TYPE_CARD },
976 { "pnm", ITEM_TYPE_NET },
977 { "qam", ITEM_TYPE_CARD },
978 { "qpsk", ITEM_TYPE_CARD },
979 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
980 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
981 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
982 { "satell", ITEM_TYPE_CARD }, /* sattelite */
983 { "screen", ITEM_TYPE_CARD },
984 { "sdp", ITEM_TYPE_NET },
985 { "sftp", ITEM_TYPE_NET },
986 { "shm", ITEM_TYPE_CARD },
987 { "smb", ITEM_TYPE_NET },
988 { "svcd", ITEM_TYPE_DISC },
989 { "tcp", ITEM_TYPE_NET },
990 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
991 { "udp", ITEM_TYPE_NET }, /* udplite too */
992 { "unsv", ITEM_TYPE_NET },
993 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
994 { "v4l", ITEM_TYPE_CARD },
995 { "vcd", ITEM_TYPE_DISC },
996 { "window", ITEM_TYPE_CARD },
998 const struct item_type_entry *e;
1000 if( !strstr( p_item->psz_uri, "://" ) )
1001 return ITEM_TYPE_FILE;
1003 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
1004 sizeof( tab[0] ), typecmp );
1005 return e ? e->i_type : ITEM_TYPE_FILE;
1008 input_item_node_t *input_item_node_Create( input_item_t *p_input )
1010 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
1016 p_node->p_item = p_input;
1017 vlc_gc_incref( p_input );
1019 p_node->p_parent = NULL;
1020 p_node->i_children = 0;
1021 p_node->pp_children = NULL;
1026 static void RecursiveNodeDelete( input_item_node_t *p_node )
1028 for( int i = 0; i < p_node->i_children; i++ )
1029 RecursiveNodeDelete( p_node->pp_children[i] );
1031 vlc_gc_decref( p_node->p_item );
1032 free( p_node->pp_children );
1036 void input_item_node_Delete( input_item_node_t *p_node )
1038 if( p_node->p_parent )
1039 for( int i = 0; i < p_node->p_parent->i_children; i++ )
1040 if( p_node->p_parent->pp_children[i] == p_node )
1042 REMOVE_ELEM( p_node->p_parent->pp_children,
1043 p_node->p_parent->i_children,
1048 RecursiveNodeDelete( p_node );
1051 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1053 input_item_node_t *p_new_child = input_item_node_Create( p_item );
1054 if( !p_new_child ) return NULL;
1055 input_item_node_AppendNode( p_node, p_new_child );
1059 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1061 assert( p_parent && p_child && p_child->p_parent == NULL );
1062 INSERT_ELEM( p_parent->pp_children,
1063 p_parent->i_children,
1064 p_parent->i_children,
1066 p_child->p_parent = p_parent;
1069 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1071 post_subitems( p_root );
1074 event.type = vlc_InputItemSubItemTreeAdded;
1075 event.u.input_item_subitem_tree_added.p_root = p_root;
1076 vlc_event_send( &p_root->p_item->event_manager, &event );
1078 input_item_node_Delete( p_root );
1081 /* Called by es_out when a new Elementary Stream is added or updated. */
1082 void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
1085 es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
1089 es_format_Copy(fmt_copy, fmt);
1090 /* XXX: we could free p_extra to save memory, we will likely not need
1091 * the decoder specific data */
1093 vlc_mutex_lock( &item->lock );
1095 for( i = 0; i < item->i_es; i++ )
1097 if (item->es[i]->i_id != fmt->i_id)
1100 /* We've found the right ES, replace it */
1101 es_format_Clean(item->es[i]);
1103 item->es[i] = fmt_copy;
1104 vlc_mutex_unlock( &item->lock );
1108 /* ES not found, insert it */
1109 TAB_APPEND(item->i_es, item->es, fmt_copy);
1110 vlc_mutex_unlock( &item->lock );