1 /*****************************************************************************
2 * item.c: input_item management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
33 #include <vlc_charset.h>
34 #include <vlc_atomic.h>
39 static int GuessType( const input_item_t
*p_item
);
41 static inline void input_item_Clean( input_item_t
*p_i
)
45 vlc_event_manager_fini( &p_i
->event_manager
);
47 free( p_i
->psz_name
);
51 vlc_mutex_destroy( &p_i
->p_stats
->lock
);
56 vlc_meta_Delete( p_i
->p_meta
);
58 for( i
= 0; i
< p_i
->i_options
; i
++ )
59 free( p_i
->ppsz_options
[i
] );
60 TAB_CLEAN( p_i
->i_options
, p_i
->ppsz_options
);
63 for( i
= 0; i
< p_i
->i_es
; i
++ )
65 es_format_Clean( p_i
->es
[i
] );
68 TAB_CLEAN( p_i
->i_es
, p_i
->es
);
70 for( i
= 0; i
< p_i
->i_epg
; i
++ )
71 vlc_epg_Delete( p_i
->pp_epg
[i
] );
72 TAB_CLEAN( p_i
->i_epg
, p_i
->pp_epg
);
74 for( i
= 0; i
< p_i
->i_categories
; i
++ )
75 info_category_Delete( p_i
->pp_categories
[i
] );
76 TAB_CLEAN( p_i
->i_categories
, p_i
->pp_categories
);
78 vlc_mutex_destroy( &p_i
->lock
);
80 void input_item_SetErrorWhenReading( input_item_t
*p_i
, bool b_error
)
84 vlc_mutex_lock( &p_i
->lock
);
86 b_changed
= p_i
->b_error_when_reading
!= b_error
;
87 p_i
->b_error_when_reading
= b_error
;
89 vlc_mutex_unlock( &p_i
->lock
);
95 event
.type
= vlc_InputItemErrorWhenReadingChanged
;
96 event
.u
.input_item_error_when_reading_changed
.new_value
= b_error
;
97 vlc_event_send( &p_i
->event_manager
, &event
);
100 void input_item_SetPreparsed( input_item_t
*p_i
, bool b_preparsed
)
102 bool b_send_event
= false;
104 vlc_mutex_lock( &p_i
->lock
);
107 p_i
->p_meta
= vlc_meta_New();
109 int status
= vlc_meta_GetStatus(p_i
->p_meta
);
112 new_status
= status
| ITEM_PREPARSED
;
114 new_status
= status
& ~ITEM_PREPARSED
;
115 if( status
!= new_status
)
117 vlc_meta_SetStatus(p_i
->p_meta
, new_status
);
121 vlc_mutex_unlock( &p_i
->lock
);
126 event
.type
= vlc_InputItemPreparsedChanged
;
127 event
.u
.input_item_preparsed_changed
.new_status
= new_status
;
128 vlc_event_send( &p_i
->event_manager
, &event
);
132 void input_item_SetArtNotFound( input_item_t
*p_i
, bool b_not_found
)
134 vlc_mutex_lock( &p_i
->lock
);
137 p_i
->p_meta
= vlc_meta_New();
139 int status
= vlc_meta_GetStatus(p_i
->p_meta
);
142 status
|= ITEM_ART_NOTFOUND
;
144 status
&= ~ITEM_ART_NOTFOUND
;
146 vlc_meta_SetStatus(p_i
->p_meta
, status
);
148 vlc_mutex_unlock( &p_i
->lock
);
151 void input_item_SetArtFetched( input_item_t
*p_i
, bool b_art_fetched
)
153 vlc_mutex_lock( &p_i
->lock
);
156 p_i
->p_meta
= vlc_meta_New();
158 int status
= vlc_meta_GetStatus(p_i
->p_meta
);
161 status
|= ITEM_ART_FETCHED
;
163 status
&= ~ITEM_ART_FETCHED
;
165 vlc_meta_SetStatus(p_i
->p_meta
, status
);
167 vlc_mutex_unlock( &p_i
->lock
);
170 void input_item_SetMeta( input_item_t
*p_i
, vlc_meta_type_t meta_type
, const char *psz_val
)
174 vlc_mutex_lock( &p_i
->lock
);
176 p_i
->p_meta
= vlc_meta_New();
177 vlc_meta_Set( p_i
->p_meta
, meta_type
, psz_val
);
178 vlc_mutex_unlock( &p_i
->lock
);
180 /* Notify interested third parties */
181 event
.type
= vlc_InputItemMetaChanged
;
182 event
.u
.input_item_meta_changed
.meta_type
= meta_type
;
183 vlc_event_send( &p_i
->event_manager
, &event
);
186 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
187 * consistent with (nearly?) all or copy funcs */
188 void input_item_CopyOptions( input_item_t
*p_parent
,
189 input_item_t
*p_child
)
191 vlc_mutex_lock( &p_parent
->lock
);
193 for( int i
= 0 ; i
< p_parent
->i_options
; i
++ )
195 if( !strcmp( p_parent
->ppsz_options
[i
], "meta-file" ) )
198 input_item_AddOption( p_child
,
199 p_parent
->ppsz_options
[i
],
200 p_parent
->optflagv
[i
] );
203 vlc_mutex_unlock( &p_parent
->lock
);
206 static void post_subitems( input_item_node_t
*p_node
)
208 for( int i
= 0; i
< p_node
->i_children
; i
++ )
211 event
.type
= vlc_InputItemSubItemAdded
;
212 event
.u
.input_item_subitem_added
.p_new_child
= p_node
->pp_children
[i
]->p_item
;
213 vlc_event_send( &p_node
->p_item
->event_manager
, &event
);
215 post_subitems( p_node
->pp_children
[i
] );
219 /* This won't hold the item, but can tell to interested third parties
220 * Like the playlist, that there is a new sub item. With this design
221 * It is not the input item's responsability to keep all the ref of
222 * the input item children. */
223 void input_item_PostSubItem( input_item_t
*p_parent
, input_item_t
*p_child
)
225 input_item_node_t
*p_node
= input_item_node_Create( p_parent
);
226 input_item_node_AppendItem( p_node
, p_child
);
227 input_item_node_PostAndDelete( p_node
);
230 bool input_item_HasErrorWhenReading( input_item_t
*p_item
)
232 vlc_mutex_lock( &p_item
->lock
);
234 bool b_error
= p_item
->b_error_when_reading
;
236 vlc_mutex_unlock( &p_item
->lock
);
241 bool input_item_MetaMatch( input_item_t
*p_i
,
242 vlc_meta_type_t meta_type
, const char *psz
)
244 vlc_mutex_lock( &p_i
->lock
);
248 vlc_mutex_unlock( &p_i
->lock
);
251 const char *psz_meta
= vlc_meta_Get( p_i
->p_meta
, meta_type
);
252 bool b_ret
= psz_meta
&& strcasestr( psz_meta
, psz
);
254 vlc_mutex_unlock( &p_i
->lock
);
259 char *input_item_GetMeta( input_item_t
*p_i
, vlc_meta_type_t meta_type
)
261 vlc_mutex_lock( &p_i
->lock
);
265 vlc_mutex_unlock( &p_i
->lock
);
270 if( vlc_meta_Get( p_i
->p_meta
, meta_type
) )
271 psz
= strdup( vlc_meta_Get( p_i
->p_meta
, meta_type
) );
273 vlc_mutex_unlock( &p_i
->lock
);
277 /* Get the title of a given item or fallback to the name if the title is empty */
278 char *input_item_GetTitleFbName( input_item_t
*p_item
)
281 vlc_mutex_lock( &p_item
->lock
);
283 if( !p_item
->p_meta
)
285 psz_ret
= p_item
->psz_name
? strdup( p_item
->psz_name
) : NULL
;
286 vlc_mutex_unlock( &p_item
->lock
);
290 const char *psz_title
= vlc_meta_Get( p_item
->p_meta
, vlc_meta_Title
);
291 if( !EMPTY_STR( psz_title
) )
292 psz_ret
= strdup( psz_title
);
294 psz_ret
= p_item
->psz_name
? strdup( p_item
->psz_name
) : NULL
;
296 vlc_mutex_unlock( &p_item
->lock
);
300 char *input_item_GetName( input_item_t
*p_item
)
302 vlc_mutex_lock( &p_item
->lock
);
304 char *psz_name
= p_item
->psz_name
? strdup( p_item
->psz_name
) : NULL
;
306 vlc_mutex_unlock( &p_item
->lock
);
309 void input_item_SetName( input_item_t
*p_item
, const char *psz_name
)
311 vlc_mutex_lock( &p_item
->lock
);
313 free( p_item
->psz_name
);
314 p_item
->psz_name
= strdup( psz_name
);
316 vlc_mutex_unlock( &p_item
->lock
);
319 char *input_item_GetURI( input_item_t
*p_i
)
321 vlc_mutex_lock( &p_i
->lock
);
323 char *psz_s
= p_i
->psz_uri
? strdup( p_i
->psz_uri
) : NULL
;
325 vlc_mutex_unlock( &p_i
->lock
);
329 void input_item_SetURI( input_item_t
*p_i
, const char *psz_uri
)
333 if( !strstr( psz_uri
, "://" )
334 || strchr( psz_uri
, ' ' ) || strchr( psz_uri
, '"' ) )
335 fprintf( stderr
, "Warning: %s(\"%s\"): file path instead of URL.\n",
338 vlc_mutex_lock( &p_i
->lock
);
339 free( p_i
->psz_uri
);
340 p_i
->psz_uri
= strdup( psz_uri
);
342 p_i
->i_type
= GuessType( p_i
);
347 if( p_i
->i_type
== ITEM_TYPE_FILE
|| p_i
->i_type
== ITEM_TYPE_DIRECTORY
)
349 const char *psz_filename
= strrchr( p_i
->psz_uri
, '/' );
351 if( psz_filename
&& *psz_filename
== '/' )
353 if( psz_filename
&& *psz_filename
)
354 p_i
->psz_name
= strdup( psz_filename
);
356 /* Make the name more readable */
359 decode_URI( p_i
->psz_name
);
360 EnsureUTF8( p_i
->psz_name
);
364 { /* Strip login and password from title */
368 vlc_UrlParse( &url
, psz_uri
, 0 );
369 if( url
.psz_protocol
)
372 r
=asprintf( &p_i
->psz_name
, "%s://%s:%d%s", url
.psz_protocol
,
373 url
.psz_host
, url
.i_port
,
374 url
.psz_path
? url
.psz_path
: "" );
376 r
=asprintf( &p_i
->psz_name
, "%s://%s%s", url
.psz_protocol
,
377 url
.psz_host
? url
.psz_host
: "",
378 url
.psz_path
? url
.psz_path
: "" );
383 r
=asprintf( &p_i
->psz_name
, "%s:%d%s", url
.psz_host
, url
.i_port
,
384 url
.psz_path
? url
.psz_path
: "" );
386 r
=asprintf( &p_i
->psz_name
, "%s%s", url
.psz_host
,
387 url
.psz_path
? url
.psz_path
: "" );
389 vlc_UrlClean( &url
);
391 p_i
->psz_name
=NULL
; /* recover from undefined value */
394 vlc_mutex_unlock( &p_i
->lock
);
397 mtime_t
input_item_GetDuration( input_item_t
*p_i
)
399 vlc_mutex_lock( &p_i
->lock
);
401 mtime_t i_duration
= p_i
->i_duration
;
403 vlc_mutex_unlock( &p_i
->lock
);
407 void input_item_SetDuration( input_item_t
*p_i
, mtime_t i_duration
)
409 bool b_send_event
= false;
411 vlc_mutex_lock( &p_i
->lock
);
412 if( p_i
->i_duration
!= i_duration
)
414 p_i
->i_duration
= i_duration
;
417 vlc_mutex_unlock( &p_i
->lock
);
423 event
.type
= vlc_InputItemDurationChanged
;
424 event
.u
.input_item_duration_changed
.new_duration
= i_duration
;
425 vlc_event_send( &p_i
->event_manager
, &event
);
430 bool input_item_IsPreparsed( input_item_t
*p_item
)
432 vlc_mutex_lock( &p_item
->lock
);
433 bool b_preparsed
= p_item
->p_meta
? ( vlc_meta_GetStatus(p_item
->p_meta
) & ITEM_PREPARSED
) != 0 : false;
434 vlc_mutex_unlock( &p_item
->lock
);
439 bool input_item_IsArtFetched( input_item_t
*p_item
)
441 vlc_mutex_lock( &p_item
->lock
);
442 bool b_fetched
= p_item
->p_meta
? ( vlc_meta_GetStatus(p_item
->p_meta
) & ITEM_ART_FETCHED
) != 0 : false;
443 vlc_mutex_unlock( &p_item
->lock
);
448 static void input_item_Destroy ( gc_object_t
*p_gc
)
450 input_item_t
*p_item
= vlc_priv( p_gc
, input_item_t
);
452 input_item_Clean( p_item
);
456 int input_item_AddOption( input_item_t
*p_input
, const char *psz_option
,
459 int err
= VLC_SUCCESS
;
461 if( psz_option
== NULL
)
464 vlc_mutex_lock( &p_input
->lock
);
465 if (flags
& VLC_INPUT_OPTION_UNIQUE
)
467 for (int i
= 0 ; i
< p_input
->i_options
; i
++)
468 if( !strcmp( p_input
->ppsz_options
[i
], psz_option
) )
472 uint8_t *flagv
= realloc (p_input
->optflagv
, p_input
->optflagc
+ 1);
478 p_input
->optflagv
= flagv
;
479 flagv
[p_input
->optflagc
++] = flags
;
481 INSERT_ELEM( p_input
->ppsz_options
, p_input
->i_options
,
482 p_input
->i_options
, strdup( psz_option
) );
484 vlc_mutex_unlock( &p_input
->lock
);
488 static info_category_t
*InputItemFindCat( input_item_t
*p_item
,
489 int *pi_index
, const char *psz_cat
)
491 vlc_assert_locked( &p_item
->lock
);
492 for( int i
= 0; i
< p_item
->i_categories
&& psz_cat
; i
++ )
494 info_category_t
*p_cat
= p_item
->pp_categories
[i
];
496 if( !strcmp( p_cat
->psz_name
, psz_cat
) )
507 * Get a info item from a given category in a given input item.
509 * \param p_i The input item to get info from
510 * \param psz_cat String representing the category for the info
511 * \param psz_name String representing the name of the desired info
512 * \return A pointer to the string with the given info if found, or an
513 * empty string otherwise. The caller should free the returned
516 char *input_item_GetInfo( input_item_t
*p_i
,
518 const char *psz_name
)
520 vlc_mutex_lock( &p_i
->lock
);
522 const info_category_t
*p_cat
= InputItemFindCat( p_i
, NULL
, psz_cat
);
525 info_t
*p_info
= info_category_FindInfo( p_cat
, NULL
, psz_name
);
526 if( p_info
&& p_info
->psz_value
)
528 char *psz_ret
= strdup( p_info
->psz_value
);
529 vlc_mutex_unlock( &p_i
->lock
);
533 vlc_mutex_unlock( &p_i
->lock
);
537 static int InputItemVaAddInfo( input_item_t
*p_i
,
539 const char *psz_name
,
540 const char *psz_format
, va_list args
)
542 vlc_assert_locked( &p_i
->lock
);
544 info_category_t
*p_cat
= InputItemFindCat( p_i
, NULL
, psz_cat
);
547 p_cat
= info_category_New( psz_cat
);
550 INSERT_ELEM( p_i
->pp_categories
, p_i
->i_categories
, p_i
->i_categories
,
553 info_t
*p_info
= info_category_VaAddInfo( p_cat
, psz_name
, psz_format
, args
);
554 if( !p_info
|| !p_info
->psz_value
)
559 static int InputItemAddInfo( input_item_t
*p_i
,
561 const char *psz_name
,
562 const char *psz_format
, ... )
566 va_start( args
, psz_format
);
567 const int i_ret
= InputItemVaAddInfo( p_i
, psz_cat
, psz_name
, psz_format
, args
);
573 int input_item_AddInfo( input_item_t
*p_i
,
575 const char *psz_name
,
576 const char *psz_format
, ... )
580 vlc_mutex_lock( &p_i
->lock
);
582 va_start( args
, psz_format
);
583 const int i_ret
= InputItemVaAddInfo( p_i
, psz_cat
, psz_name
, psz_format
, args
);
586 vlc_mutex_unlock( &p_i
->lock
);
593 event
.type
= vlc_InputItemInfoChanged
;
594 vlc_event_send( &p_i
->event_manager
, &event
);
599 int input_item_DelInfo( input_item_t
*p_i
,
601 const char *psz_name
)
603 vlc_mutex_lock( &p_i
->lock
);
605 info_category_t
*p_cat
= InputItemFindCat( p_i
, &i_cat
, psz_cat
);
608 vlc_mutex_unlock( &p_i
->lock
);
614 /* Remove a specific info */
615 int i_ret
= info_category_DeleteInfo( p_cat
, psz_name
);
618 vlc_mutex_unlock( &p_i
->lock
);
624 /* Remove the complete categorie */
625 info_category_Delete( p_cat
);
626 REMOVE_ELEM( p_i
->pp_categories
, p_i
->i_categories
, i_cat
);
628 vlc_mutex_unlock( &p_i
->lock
);
632 event
.type
= vlc_InputItemInfoChanged
;
633 vlc_event_send( &p_i
->event_manager
, &event
);
637 void input_item_ReplaceInfos( input_item_t
*p_item
, info_category_t
*p_cat
)
639 vlc_mutex_lock( &p_item
->lock
);
641 info_category_t
*p_old
= InputItemFindCat( p_item
, &i_cat
, p_cat
->psz_name
);
644 info_category_Delete( p_old
);
645 p_item
->pp_categories
[i_cat
] = p_cat
;
649 INSERT_ELEM( p_item
->pp_categories
, p_item
->i_categories
, p_item
->i_categories
,
652 vlc_mutex_unlock( &p_item
->lock
);
656 event
.type
= vlc_InputItemInfoChanged
;
657 vlc_event_send( &p_item
->event_manager
, &event
);
659 void input_item_MergeInfos( input_item_t
*p_item
, info_category_t
*p_cat
)
661 vlc_mutex_lock( &p_item
->lock
);
662 info_category_t
*p_old
= InputItemFindCat( p_item
, NULL
, p_cat
->psz_name
);
665 for( int i
= 0; i
< p_cat
->i_infos
; i
++ )
666 info_category_ReplaceInfo( p_old
, p_cat
->pp_infos
[i
] );
667 TAB_CLEAN( p_cat
->i_infos
, p_cat
->pp_infos
);
668 info_category_Delete( p_cat
);
672 INSERT_ELEM( p_item
->pp_categories
, p_item
->i_categories
, p_item
->i_categories
,
675 vlc_mutex_unlock( &p_item
->lock
);
679 event
.type
= vlc_InputItemInfoChanged
;
680 vlc_event_send( &p_item
->event_manager
, &event
);
684 void input_item_SetEpg( input_item_t
*p_item
, const vlc_epg_t
*p_update
)
686 vlc_mutex_lock( &p_item
->lock
);
689 vlc_epg_t
*p_epg
= NULL
;
690 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
692 vlc_epg_t
*p_tmp
= p_item
->pp_epg
[i
];
694 if( (p_tmp
->psz_name
== NULL
) != (p_update
->psz_name
== NULL
) )
696 if( p_tmp
->psz_name
&& p_update
->psz_name
&& strcmp(p_tmp
->psz_name
, p_update
->psz_name
) )
706 p_epg
= vlc_epg_New( p_update
->psz_name
);
708 TAB_APPEND( p_item
->i_epg
, p_item
->pp_epg
, p_epg
);
711 vlc_epg_Merge( p_epg
, p_update
);
713 vlc_mutex_unlock( &p_item
->lock
);
720 if( asprintf( &psz_epg
, "EPG %s", p_epg
->psz_name
? p_epg
->psz_name
: "unknown" ) < 0 )
723 input_item_DelInfo( p_item
, psz_epg
, NULL
);
725 vlc_mutex_lock( &p_item
->lock
);
726 for( int i
= 0; i
< p_epg
->i_event
; i
++ )
728 const vlc_epg_event_t
*p_evt
= p_epg
->pp_event
[i
];
729 time_t t_start
= (time_t)p_evt
->i_start
;
733 localtime_r( &t_start
, &tm_start
);
735 snprintf( psz_start
, sizeof(psz_start
), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
736 1900 + tm_start
.tm_year
, 1 + tm_start
.tm_mon
, tm_start
.tm_mday
,
737 tm_start
.tm_hour
, tm_start
.tm_min
, tm_start
.tm_sec
);
738 if( p_evt
->psz_short_description
|| p_evt
->psz_description
)
739 InputItemAddInfo( p_item
, psz_epg
, psz_start
, "%s (%2.2d:%2.2d) - %s %s",
741 p_evt
->i_duration
/60/60, (p_evt
->i_duration
/60)%60,
742 p_evt
->psz_short_description
? p_evt
->psz_short_description
: "" ,
743 p_evt
->psz_description
? p_evt
->psz_description
: "" );
745 InputItemAddInfo( p_item
, psz_epg
, psz_start
, "%s (%2.2d:%2.2d)",
747 p_evt
->i_duration
/60/60, (p_evt
->i_duration
/60)%60 );
749 vlc_mutex_unlock( &p_item
->lock
);
754 if( p_epg
->i_event
> 0 )
756 vlc_event_t event
= { .type
= vlc_InputItemInfoChanged
, };
757 vlc_event_send( &p_item
->event_manager
, &event
);
761 void input_item_SetEpgOffline( input_item_t
*p_item
)
763 vlc_mutex_lock( &p_item
->lock
);
764 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
765 vlc_epg_SetCurrent( p_item
->pp_epg
[i
], -1 );
766 vlc_mutex_unlock( &p_item
->lock
);
769 vlc_mutex_lock( &p_item
->lock
);
770 const int i_epg_info
= p_item
->i_epg
;
771 char *ppsz_epg_info
[i_epg_info
];
772 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
774 const vlc_epg_t
*p_epg
= p_item
->pp_epg
[i
];
775 if( asprintf( &ppsz_epg_info
[i
], "EPG %s", p_epg
->psz_name
? p_epg
->psz_name
: "unknown" ) < 0 )
776 ppsz_epg_info
[i
] = NULL
;
778 vlc_mutex_unlock( &p_item
->lock
);
780 for( int i
= 0; i
< i_epg_info
; i
++ )
782 if( !ppsz_epg_info
[i
] )
784 input_item_DelInfo( p_item
, ppsz_epg_info
[i
], NULL
);
785 free( ppsz_epg_info
[i
] );
789 vlc_event_t event
= { .type
= vlc_InputItemInfoChanged
, };
790 vlc_event_send( &p_item
->event_manager
, &event
);
793 input_item_t
*input_item_NewExt( const char *psz_uri
,
794 const char *psz_name
,
796 const char *const *ppsz_options
,
797 unsigned i_option_flags
,
800 return input_item_NewWithType( psz_uri
, psz_name
,
801 i_options
, ppsz_options
, i_option_flags
,
802 i_duration
, ITEM_TYPE_UNKNOWN
);
807 input_item_NewWithType( const char *psz_uri
, const char *psz_name
,
808 int i_options
, const char *const *ppsz_options
,
809 unsigned flags
, mtime_t duration
, int type
)
811 static vlc_atomic_t last_input_id
= VLC_ATOMIC_INIT(0);
813 input_item_t
* p_input
= calloc( 1, sizeof( *p_input
) );
816 vlc_event_manager_t
* p_em
= &p_input
->event_manager
;
818 p_input
->i_id
= vlc_atomic_inc(&last_input_id
);
819 vlc_gc_init( p_input
, input_item_Destroy
);
820 vlc_mutex_init( &p_input
->lock
);
822 p_input
->psz_name
= NULL
;
824 input_item_SetName( p_input
, psz_name
);
826 p_input
->psz_uri
= NULL
;
828 input_item_SetURI( p_input
, psz_uri
);
830 p_input
->i_type
= ITEM_TYPE_UNKNOWN
;
832 TAB_INIT( p_input
->i_options
, p_input
->ppsz_options
);
833 p_input
->optflagc
= 0;
834 p_input
->optflagv
= NULL
;
835 for( int i
= 0; i
< i_options
; i
++ )
836 input_item_AddOption( p_input
, ppsz_options
[i
], flags
);
838 p_input
->i_duration
= duration
;
839 TAB_INIT( p_input
->i_categories
, p_input
->pp_categories
);
840 TAB_INIT( p_input
->i_es
, p_input
->es
);
841 p_input
->p_stats
= NULL
;
842 p_input
->i_nb_played
= 0;
843 p_input
->p_meta
= NULL
;
844 TAB_INIT( p_input
->i_epg
, p_input
->pp_epg
);
846 vlc_event_manager_init( p_em
, p_input
);
847 vlc_event_manager_register_event_type( p_em
, vlc_InputItemMetaChanged
);
848 vlc_event_manager_register_event_type( p_em
, vlc_InputItemSubItemAdded
);
849 vlc_event_manager_register_event_type( p_em
, vlc_InputItemSubItemTreeAdded
);
850 vlc_event_manager_register_event_type( p_em
, vlc_InputItemDurationChanged
);
851 vlc_event_manager_register_event_type( p_em
, vlc_InputItemPreparsedChanged
);
852 vlc_event_manager_register_event_type( p_em
, vlc_InputItemNameChanged
);
853 vlc_event_manager_register_event_type( p_em
, vlc_InputItemInfoChanged
);
854 vlc_event_manager_register_event_type( p_em
, vlc_InputItemErrorWhenReadingChanged
);
856 if( type
!= ITEM_TYPE_UNKNOWN
)
857 p_input
->i_type
= type
;
858 p_input
->b_fixed_name
= false;
859 p_input
->b_error_when_reading
= false;
863 input_item_t
*input_item_Copy( input_item_t
*p_input
)
865 vlc_mutex_lock( &p_input
->lock
);
867 input_item_t
*p_new_input
=
868 input_item_NewWithType( p_input
->psz_uri
, p_input
->psz_name
,
869 0, NULL
, 0, p_input
->i_duration
,
874 for( int i
= 0 ; i
< p_input
->i_options
; i
++ )
876 input_item_AddOption( p_new_input
,
877 p_input
->ppsz_options
[i
],
878 p_input
->optflagv
[i
] );
881 if( p_input
->p_meta
)
883 p_new_input
->p_meta
= vlc_meta_New();
884 vlc_meta_Merge( p_new_input
->p_meta
, p_input
->p_meta
);
888 vlc_mutex_unlock( &p_input
->lock
);
893 struct item_type_entry
895 const char psz_scheme
[7];
899 static int typecmp( const void *key
, const void *entry
)
901 const struct item_type_entry
*type
= entry
;
902 const char *uri
= key
, *scheme
= type
->psz_scheme
;
904 return strncmp( uri
, scheme
, strlen( scheme
) );
907 /* Guess the type of the item using the beginning of the mrl */
908 static int GuessType( const input_item_t
*p_item
)
910 static const struct item_type_entry tab
[] =
911 { /* /!\ Alphabetical order /!\ */
912 /* Short match work, not just exact match */
913 { "alsa", ITEM_TYPE_CARD
},
914 { "atsc", ITEM_TYPE_CARD
},
915 { "bd", ITEM_TYPE_DISC
},
916 { "cable", ITEM_TYPE_CARD
},
917 { "cdda", ITEM_TYPE_CDDA
},
918 { "cqam", ITEM_TYPE_CARD
},
919 { "dc1394", ITEM_TYPE_CARD
},
920 { "dccp", ITEM_TYPE_NET
},
921 { "deckli", ITEM_TYPE_CARD
}, /* decklink */
922 { "dir", ITEM_TYPE_DIRECTORY
},
923 { "dshow", ITEM_TYPE_CARD
},
924 { "dv", ITEM_TYPE_CARD
},
925 { "dvb", ITEM_TYPE_CARD
},
926 { "dvd", ITEM_TYPE_DISC
},
927 { "dtv", ITEM_TYPE_CARD
},
928 { "eyetv", ITEM_TYPE_CARD
},
929 { "fd", ITEM_TYPE_UNKNOWN
},
930 { "ftp", ITEM_TYPE_NET
},
931 { "http", ITEM_TYPE_NET
},
932 { "icyx", ITEM_TYPE_NET
},
933 { "imem", ITEM_TYPE_UNKNOWN
},
934 { "itpc", ITEM_TYPE_NET
},
935 { "jack", ITEM_TYPE_CARD
},
936 { "linsys", ITEM_TYPE_CARD
},
937 { "live", ITEM_TYPE_NET
}, /* livedotcom */
938 { "mms", ITEM_TYPE_NET
},
939 { "mtp", ITEM_TYPE_DISC
},
940 { "ofdm", ITEM_TYPE_CARD
},
941 { "oss", ITEM_TYPE_CARD
},
942 { "pnm", ITEM_TYPE_NET
},
943 { "pvr", ITEM_TYPE_CARD
},
944 { "qam", ITEM_TYPE_CARD
},
945 { "qpsk", ITEM_TYPE_CARD
},
946 { "qtcapt", ITEM_TYPE_CARD
}, /* qtcapture */
947 { "raw139", ITEM_TYPE_CARD
}, /* raw1394 */
948 { "rt", ITEM_TYPE_NET
}, /* rtp, rtsp, rtmp */
949 { "satell", ITEM_TYPE_CARD
}, /* sattelite */
950 { "screen", ITEM_TYPE_CARD
},
951 { "sdp", ITEM_TYPE_NET
},
952 { "sftp", ITEM_TYPE_NET
},
953 { "shm", ITEM_TYPE_CARD
},
954 { "smb", ITEM_TYPE_NET
},
955 { "svcd", ITEM_TYPE_DISC
},
956 { "tcp", ITEM_TYPE_NET
},
957 { "terres", ITEM_TYPE_CARD
}, /* terrestrial */
958 { "udp", ITEM_TYPE_NET
}, /* udplite too */
959 { "unsv", ITEM_TYPE_NET
},
960 { "usdigi", ITEM_TYPE_CARD
}, /* usdigital */
961 { "v4l", ITEM_TYPE_CARD
},
962 { "vcd", ITEM_TYPE_DISC
},
963 { "window", ITEM_TYPE_CARD
},
965 const struct item_type_entry
*e
;
967 if( !strstr( p_item
->psz_uri
, "://" ) )
968 return ITEM_TYPE_FILE
;
970 e
= bsearch( p_item
->psz_uri
, tab
, sizeof( tab
) / sizeof( tab
[0] ),
971 sizeof( tab
[0] ), typecmp
);
972 return e
? e
->i_type
: ITEM_TYPE_FILE
;
975 input_item_node_t
*input_item_node_Create( input_item_t
*p_input
)
977 input_item_node_t
* p_node
= malloc( sizeof( input_item_node_t
) );
983 p_node
->p_item
= p_input
;
984 vlc_gc_incref( p_input
);
986 p_node
->p_parent
= NULL
;
987 p_node
->i_children
= 0;
988 p_node
->pp_children
= NULL
;
993 static void RecursiveNodeDelete( input_item_node_t
*p_node
)
995 for( int i
= 0; i
< p_node
->i_children
; i
++ )
996 RecursiveNodeDelete( p_node
->pp_children
[i
] );
998 vlc_gc_decref( p_node
->p_item
);
999 free( p_node
->pp_children
);
1003 void input_item_node_Delete( input_item_node_t
*p_node
)
1005 if( p_node
->p_parent
)
1006 for( int i
= 0; i
< p_node
->p_parent
->i_children
; i
++ )
1007 if( p_node
->p_parent
->pp_children
[i
] == p_node
)
1009 REMOVE_ELEM( p_node
->p_parent
->pp_children
,
1010 p_node
->p_parent
->i_children
,
1015 RecursiveNodeDelete( p_node
);
1018 input_item_node_t
*input_item_node_AppendItem( input_item_node_t
*p_node
, input_item_t
*p_item
)
1020 input_item_node_t
*p_new_child
= input_item_node_Create( p_item
);
1021 if( !p_new_child
) return NULL
;
1022 input_item_node_AppendNode( p_node
, p_new_child
);
1026 void input_item_node_AppendNode( input_item_node_t
*p_parent
, input_item_node_t
*p_child
)
1028 assert( p_parent
&& p_child
&& p_child
->p_parent
== NULL
);
1029 INSERT_ELEM( p_parent
->pp_children
,
1030 p_parent
->i_children
,
1031 p_parent
->i_children
,
1033 p_child
->p_parent
= p_parent
;
1036 void input_item_node_PostAndDelete( input_item_node_t
*p_root
)
1038 post_subitems( p_root
);
1041 event
.type
= vlc_InputItemSubItemTreeAdded
;
1042 event
.u
.input_item_subitem_tree_added
.p_root
= p_root
;
1043 vlc_event_send( &p_root
->p_item
->event_manager
, &event
);
1045 input_item_node_Delete( p_root
);
1048 /* Called by es_out when a new Elementary Stream is added or updated. */
1049 void input_item_UpdateTracksInfo(input_item_t
*item
, const es_format_t
*fmt
)
1052 es_format_t
*fmt_copy
= malloc(sizeof *fmt_copy
);
1056 es_format_Copy(fmt_copy
, fmt
);
1057 /* XXX: we could free p_extra to save memory, we will likely not need
1058 * the decoder specific data */
1060 vlc_mutex_lock( &item
->lock
);
1062 for( i
= 0; i
< item
->i_es
; i
++ )
1064 if (item
->es
[i
]->i_id
!= fmt
->i_id
)
1067 /* We've found the right ES, replace it */
1068 es_format_Clean(item
->es
[i
]);
1070 item
->es
[i
] = fmt_copy
;
1071 vlc_mutex_unlock( &item
->lock
);
1075 /* ES not found, insert it */
1076 TAB_APPEND(item
->i_es
, item
->es
, fmt_copy
);
1077 vlc_mutex_unlock( &item
->lock
);