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 uint8_t *flagv
= realloc (p_input
->optflagv
, p_input
->optflagc
+ 1);
482 p_input
->optflagv
= flagv
;
483 flagv
[p_input
->optflagc
++] = flags
;
485 INSERT_ELEM( p_input
->ppsz_options
, p_input
->i_options
,
486 p_input
->i_options
, strdup( psz_option
) );
488 vlc_mutex_unlock( &p_input
->lock
);
492 static info_category_t
*InputItemFindCat( input_item_t
*p_item
,
493 int *pi_index
, const char *psz_cat
)
495 vlc_assert_locked( &p_item
->lock
);
496 for( int i
= 0; i
< p_item
->i_categories
&& psz_cat
; i
++ )
498 info_category_t
*p_cat
= p_item
->pp_categories
[i
];
500 if( !strcmp( p_cat
->psz_name
, psz_cat
) )
511 * Get a info item from a given category in a given input item.
513 * \param p_i The input item to get info from
514 * \param psz_cat String representing the category for the info
515 * \param psz_name String representing the name of the desired info
516 * \return A pointer to the string with the given info if found, or an
517 * empty string otherwise. The caller should free the returned
520 char *input_item_GetInfo( input_item_t
*p_i
,
522 const char *psz_name
)
524 vlc_mutex_lock( &p_i
->lock
);
526 const info_category_t
*p_cat
= InputItemFindCat( p_i
, NULL
, psz_cat
);
529 info_t
*p_info
= info_category_FindInfo( p_cat
, NULL
, psz_name
);
530 if( p_info
&& p_info
->psz_value
)
532 char *psz_ret
= strdup( p_info
->psz_value
);
533 vlc_mutex_unlock( &p_i
->lock
);
537 vlc_mutex_unlock( &p_i
->lock
);
541 static int InputItemVaAddInfo( input_item_t
*p_i
,
543 const char *psz_name
,
544 const char *psz_format
, va_list args
)
546 vlc_assert_locked( &p_i
->lock
);
548 info_category_t
*p_cat
= InputItemFindCat( p_i
, NULL
, psz_cat
);
551 p_cat
= info_category_New( psz_cat
);
554 INSERT_ELEM( p_i
->pp_categories
, p_i
->i_categories
, p_i
->i_categories
,
557 info_t
*p_info
= info_category_VaAddInfo( p_cat
, psz_name
, psz_format
, args
);
558 if( !p_info
|| !p_info
->psz_value
)
563 static int InputItemAddInfo( input_item_t
*p_i
,
565 const char *psz_name
,
566 const char *psz_format
, ... )
570 va_start( args
, psz_format
);
571 const int i_ret
= InputItemVaAddInfo( p_i
, psz_cat
, psz_name
, psz_format
, args
);
577 int input_item_AddInfo( input_item_t
*p_i
,
579 const char *psz_name
,
580 const char *psz_format
, ... )
584 vlc_mutex_lock( &p_i
->lock
);
586 va_start( args
, psz_format
);
587 const int i_ret
= InputItemVaAddInfo( p_i
, psz_cat
, psz_name
, psz_format
, args
);
590 vlc_mutex_unlock( &p_i
->lock
);
597 event
.type
= vlc_InputItemInfoChanged
;
598 vlc_event_send( &p_i
->event_manager
, &event
);
603 int input_item_DelInfo( input_item_t
*p_i
,
605 const char *psz_name
)
607 vlc_mutex_lock( &p_i
->lock
);
609 info_category_t
*p_cat
= InputItemFindCat( p_i
, &i_cat
, psz_cat
);
612 vlc_mutex_unlock( &p_i
->lock
);
618 /* Remove a specific info */
619 int i_ret
= info_category_DeleteInfo( p_cat
, psz_name
);
622 vlc_mutex_unlock( &p_i
->lock
);
628 /* Remove the complete categorie */
629 info_category_Delete( p_cat
);
630 REMOVE_ELEM( p_i
->pp_categories
, p_i
->i_categories
, i_cat
);
632 vlc_mutex_unlock( &p_i
->lock
);
636 event
.type
= vlc_InputItemInfoChanged
;
637 vlc_event_send( &p_i
->event_manager
, &event
);
641 void input_item_ReplaceInfos( input_item_t
*p_item
, info_category_t
*p_cat
)
643 vlc_mutex_lock( &p_item
->lock
);
645 info_category_t
*p_old
= InputItemFindCat( p_item
, &i_cat
, p_cat
->psz_name
);
648 info_category_Delete( p_old
);
649 p_item
->pp_categories
[i_cat
] = p_cat
;
653 INSERT_ELEM( p_item
->pp_categories
, p_item
->i_categories
, p_item
->i_categories
,
656 vlc_mutex_unlock( &p_item
->lock
);
660 event
.type
= vlc_InputItemInfoChanged
;
661 vlc_event_send( &p_item
->event_manager
, &event
);
663 void input_item_MergeInfos( input_item_t
*p_item
, info_category_t
*p_cat
)
665 vlc_mutex_lock( &p_item
->lock
);
666 info_category_t
*p_old
= InputItemFindCat( p_item
, NULL
, p_cat
->psz_name
);
669 for( int i
= 0; i
< p_cat
->i_infos
; i
++ )
670 info_category_ReplaceInfo( p_old
, p_cat
->pp_infos
[i
] );
671 TAB_CLEAN( p_cat
->i_infos
, p_cat
->pp_infos
);
672 info_category_Delete( p_cat
);
676 INSERT_ELEM( p_item
->pp_categories
, p_item
->i_categories
, p_item
->i_categories
,
679 vlc_mutex_unlock( &p_item
->lock
);
683 event
.type
= vlc_InputItemInfoChanged
;
684 vlc_event_send( &p_item
->event_manager
, &event
);
688 void input_item_SetEpg( input_item_t
*p_item
, const vlc_epg_t
*p_update
)
690 vlc_mutex_lock( &p_item
->lock
);
693 vlc_epg_t
*p_epg
= NULL
;
694 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
696 vlc_epg_t
*p_tmp
= p_item
->pp_epg
[i
];
698 if( (p_tmp
->psz_name
== NULL
) != (p_update
->psz_name
== NULL
) )
700 if( p_tmp
->psz_name
&& p_update
->psz_name
&& strcmp(p_tmp
->psz_name
, p_update
->psz_name
) )
710 p_epg
= vlc_epg_New( p_update
->psz_name
);
712 TAB_APPEND( p_item
->i_epg
, p_item
->pp_epg
, p_epg
);
715 vlc_epg_Merge( p_epg
, p_update
);
717 vlc_mutex_unlock( &p_item
->lock
);
724 if( asprintf( &psz_epg
, "EPG %s", p_epg
->psz_name
? p_epg
->psz_name
: "unknown" ) < 0 )
727 input_item_DelInfo( p_item
, psz_epg
, NULL
);
729 vlc_mutex_lock( &p_item
->lock
);
730 for( int i
= 0; i
< p_epg
->i_event
; i
++ )
732 const vlc_epg_event_t
*p_evt
= p_epg
->pp_event
[i
];
733 time_t t_start
= (time_t)p_evt
->i_start
;
737 localtime_r( &t_start
, &tm_start
);
739 snprintf( psz_start
, sizeof(psz_start
), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
740 1900 + tm_start
.tm_year
, 1 + tm_start
.tm_mon
, tm_start
.tm_mday
,
741 tm_start
.tm_hour
, tm_start
.tm_min
, tm_start
.tm_sec
);
742 if( p_evt
->psz_short_description
|| p_evt
->psz_description
)
743 InputItemAddInfo( p_item
, psz_epg
, psz_start
, "%s (%2.2d:%2.2d) - %s %s",
745 p_evt
->i_duration
/60/60, (p_evt
->i_duration
/60)%60,
746 p_evt
->psz_short_description
? p_evt
->psz_short_description
: "" ,
747 p_evt
->psz_description
? p_evt
->psz_description
: "" );
749 InputItemAddInfo( p_item
, psz_epg
, psz_start
, "%s (%2.2d:%2.2d)",
751 p_evt
->i_duration
/60/60, (p_evt
->i_duration
/60)%60 );
753 vlc_mutex_unlock( &p_item
->lock
);
758 if( p_epg
->i_event
> 0 )
760 vlc_event_t event
= { .type
= vlc_InputItemInfoChanged
, };
761 vlc_event_send( &p_item
->event_manager
, &event
);
765 void input_item_SetEpgOffline( input_item_t
*p_item
)
767 vlc_mutex_lock( &p_item
->lock
);
768 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
769 vlc_epg_SetCurrent( p_item
->pp_epg
[i
], -1 );
770 vlc_mutex_unlock( &p_item
->lock
);
773 vlc_mutex_lock( &p_item
->lock
);
774 const int i_epg_info
= p_item
->i_epg
;
775 char *ppsz_epg_info
[i_epg_info
];
776 for( int i
= 0; i
< p_item
->i_epg
; i
++ )
778 const vlc_epg_t
*p_epg
= p_item
->pp_epg
[i
];
779 if( asprintf( &ppsz_epg_info
[i
], "EPG %s", p_epg
->psz_name
? p_epg
->psz_name
: "unknown" ) < 0 )
780 ppsz_epg_info
[i
] = NULL
;
782 vlc_mutex_unlock( &p_item
->lock
);
784 for( int i
= 0; i
< i_epg_info
; i
++ )
786 if( !ppsz_epg_info
[i
] )
788 input_item_DelInfo( p_item
, ppsz_epg_info
[i
], NULL
);
789 free( ppsz_epg_info
[i
] );
793 vlc_event_t event
= { .type
= vlc_InputItemInfoChanged
, };
794 vlc_event_send( &p_item
->event_manager
, &event
);
797 input_item_t
*input_item_NewExt( const char *psz_uri
,
798 const char *psz_name
,
800 const char *const *ppsz_options
,
801 unsigned i_option_flags
,
804 return input_item_NewWithType( psz_uri
, psz_name
,
805 i_options
, ppsz_options
, i_option_flags
,
806 i_duration
, ITEM_TYPE_UNKNOWN
);
811 input_item_NewWithType( const char *psz_uri
, const char *psz_name
,
812 int i_options
, const char *const *ppsz_options
,
813 unsigned flags
, mtime_t duration
, int type
)
815 static atomic_uint last_input_id
= ATOMIC_VAR_INIT(0);
817 input_item_owner_t
*owner
= calloc( 1, sizeof( *owner
) );
818 if( unlikely(owner
== NULL
) )
821 atomic_init( &owner
->refs
, 1 );
823 input_item_t
*p_input
= &owner
->item
;
824 vlc_event_manager_t
* p_em
= &p_input
->event_manager
;
826 p_input
->i_id
= atomic_fetch_add(&last_input_id
, 1);
827 vlc_mutex_init( &p_input
->lock
);
829 p_input
->psz_name
= NULL
;
831 input_item_SetName( p_input
, psz_name
);
833 p_input
->psz_uri
= NULL
;
835 input_item_SetURI( p_input
, psz_uri
);
837 p_input
->i_type
= ITEM_TYPE_UNKNOWN
;
839 TAB_INIT( p_input
->i_options
, p_input
->ppsz_options
);
840 p_input
->optflagc
= 0;
841 p_input
->optflagv
= NULL
;
842 for( int i
= 0; i
< i_options
; i
++ )
843 input_item_AddOption( p_input
, ppsz_options
[i
], flags
);
845 p_input
->i_duration
= duration
;
846 TAB_INIT( p_input
->i_categories
, p_input
->pp_categories
);
847 TAB_INIT( p_input
->i_es
, p_input
->es
);
848 p_input
->p_stats
= NULL
;
849 p_input
->i_nb_played
= 0;
850 p_input
->p_meta
= NULL
;
851 TAB_INIT( p_input
->i_epg
, p_input
->pp_epg
);
853 vlc_event_manager_init( p_em
, p_input
);
854 vlc_event_manager_register_event_type( p_em
, vlc_InputItemMetaChanged
);
855 vlc_event_manager_register_event_type( p_em
, vlc_InputItemSubItemAdded
);
856 vlc_event_manager_register_event_type( p_em
, vlc_InputItemSubItemTreeAdded
);
857 vlc_event_manager_register_event_type( p_em
, vlc_InputItemDurationChanged
);
858 vlc_event_manager_register_event_type( p_em
, vlc_InputItemPreparsedChanged
);
859 vlc_event_manager_register_event_type( p_em
, vlc_InputItemNameChanged
);
860 vlc_event_manager_register_event_type( p_em
, vlc_InputItemInfoChanged
);
861 vlc_event_manager_register_event_type( p_em
, vlc_InputItemErrorWhenReadingChanged
);
863 if( type
!= ITEM_TYPE_UNKNOWN
)
864 p_input
->i_type
= type
;
865 p_input
->b_fixed_name
= false;
866 p_input
->b_error_when_reading
= false;
870 input_item_t
*input_item_Copy( input_item_t
*p_input
)
872 vlc_mutex_lock( &p_input
->lock
);
874 input_item_t
*p_new_input
=
875 input_item_NewWithType( p_input
->psz_uri
, p_input
->psz_name
,
876 0, NULL
, 0, p_input
->i_duration
,
881 for( int i
= 0 ; i
< p_input
->i_options
; i
++ )
883 input_item_AddOption( p_new_input
,
884 p_input
->ppsz_options
[i
],
885 p_input
->optflagv
[i
] );
888 if( p_input
->p_meta
)
890 p_new_input
->p_meta
= vlc_meta_New();
891 vlc_meta_Merge( p_new_input
->p_meta
, p_input
->p_meta
);
895 vlc_mutex_unlock( &p_input
->lock
);
900 struct item_type_entry
902 const char psz_scheme
[7];
906 static int typecmp( const void *key
, const void *entry
)
908 const struct item_type_entry
*type
= entry
;
909 const char *uri
= key
, *scheme
= type
->psz_scheme
;
911 return strncmp( uri
, scheme
, strlen( scheme
) );
914 /* Guess the type of the item using the beginning of the mrl */
915 static int GuessType( const input_item_t
*p_item
)
917 static const struct item_type_entry tab
[] =
918 { /* /!\ Alphabetical order /!\ */
919 /* Short match work, not just exact match */
920 { "alsa", ITEM_TYPE_CARD
},
921 { "atsc", ITEM_TYPE_CARD
},
922 { "bd", ITEM_TYPE_DISC
},
923 { "cable", ITEM_TYPE_CARD
},
924 { "cdda", ITEM_TYPE_CDDA
},
925 { "cqam", ITEM_TYPE_CARD
},
926 { "dc1394", ITEM_TYPE_CARD
},
927 { "dccp", ITEM_TYPE_NET
},
928 { "deckli", ITEM_TYPE_CARD
}, /* decklink */
929 { "dir", ITEM_TYPE_DIRECTORY
},
930 { "dshow", ITEM_TYPE_CARD
},
931 { "dv", ITEM_TYPE_CARD
},
932 { "dvb", ITEM_TYPE_CARD
},
933 { "dvd", ITEM_TYPE_DISC
},
934 { "dtv", ITEM_TYPE_CARD
},
935 { "eyetv", ITEM_TYPE_CARD
},
936 { "fd", ITEM_TYPE_UNKNOWN
},
937 { "ftp", ITEM_TYPE_NET
},
938 { "http", ITEM_TYPE_NET
},
939 { "icyx", ITEM_TYPE_NET
},
940 { "imem", ITEM_TYPE_UNKNOWN
},
941 { "itpc", ITEM_TYPE_NET
},
942 { "jack", ITEM_TYPE_CARD
},
943 { "linsys", ITEM_TYPE_CARD
},
944 { "live", ITEM_TYPE_NET
}, /* livedotcom */
945 { "mms", ITEM_TYPE_NET
},
946 { "mtp", ITEM_TYPE_DISC
},
947 { "ofdm", ITEM_TYPE_CARD
},
948 { "oss", ITEM_TYPE_CARD
},
949 { "pnm", ITEM_TYPE_NET
},
950 { "qam", ITEM_TYPE_CARD
},
951 { "qpsk", ITEM_TYPE_CARD
},
952 { "qtcapt", ITEM_TYPE_CARD
}, /* qtcapture */
953 { "raw139", ITEM_TYPE_CARD
}, /* raw1394 */
954 { "rt", ITEM_TYPE_NET
}, /* rtp, rtsp, rtmp */
955 { "satell", ITEM_TYPE_CARD
}, /* sattelite */
956 { "screen", ITEM_TYPE_CARD
},
957 { "sdp", ITEM_TYPE_NET
},
958 { "sftp", ITEM_TYPE_NET
},
959 { "shm", ITEM_TYPE_CARD
},
960 { "smb", ITEM_TYPE_NET
},
961 { "svcd", ITEM_TYPE_DISC
},
962 { "tcp", ITEM_TYPE_NET
},
963 { "terres", ITEM_TYPE_CARD
}, /* terrestrial */
964 { "udp", ITEM_TYPE_NET
}, /* udplite too */
965 { "unsv", ITEM_TYPE_NET
},
966 { "usdigi", ITEM_TYPE_CARD
}, /* usdigital */
967 { "v4l", ITEM_TYPE_CARD
},
968 { "vcd", ITEM_TYPE_DISC
},
969 { "window", ITEM_TYPE_CARD
},
971 const struct item_type_entry
*e
;
973 if( !strstr( p_item
->psz_uri
, "://" ) )
974 return ITEM_TYPE_FILE
;
976 e
= bsearch( p_item
->psz_uri
, tab
, sizeof( tab
) / sizeof( tab
[0] ),
977 sizeof( tab
[0] ), typecmp
);
978 return e
? e
->i_type
: ITEM_TYPE_FILE
;
981 input_item_node_t
*input_item_node_Create( input_item_t
*p_input
)
983 input_item_node_t
* p_node
= malloc( sizeof( input_item_node_t
) );
989 p_node
->p_item
= p_input
;
990 vlc_gc_incref( p_input
);
992 p_node
->p_parent
= NULL
;
993 p_node
->i_children
= 0;
994 p_node
->pp_children
= NULL
;
999 static void RecursiveNodeDelete( input_item_node_t
*p_node
)
1001 for( int i
= 0; i
< p_node
->i_children
; i
++ )
1002 RecursiveNodeDelete( p_node
->pp_children
[i
] );
1004 vlc_gc_decref( p_node
->p_item
);
1005 free( p_node
->pp_children
);
1009 void input_item_node_Delete( input_item_node_t
*p_node
)
1011 if( p_node
->p_parent
)
1012 for( int i
= 0; i
< p_node
->p_parent
->i_children
; i
++ )
1013 if( p_node
->p_parent
->pp_children
[i
] == p_node
)
1015 REMOVE_ELEM( p_node
->p_parent
->pp_children
,
1016 p_node
->p_parent
->i_children
,
1021 RecursiveNodeDelete( p_node
);
1024 input_item_node_t
*input_item_node_AppendItem( input_item_node_t
*p_node
, input_item_t
*p_item
)
1026 input_item_node_t
*p_new_child
= input_item_node_Create( p_item
);
1027 if( !p_new_child
) return NULL
;
1028 input_item_node_AppendNode( p_node
, p_new_child
);
1032 void input_item_node_AppendNode( input_item_node_t
*p_parent
, input_item_node_t
*p_child
)
1034 assert( p_parent
&& p_child
&& p_child
->p_parent
== NULL
);
1035 INSERT_ELEM( p_parent
->pp_children
,
1036 p_parent
->i_children
,
1037 p_parent
->i_children
,
1039 p_child
->p_parent
= p_parent
;
1042 void input_item_node_PostAndDelete( input_item_node_t
*p_root
)
1044 post_subitems( p_root
);
1047 event
.type
= vlc_InputItemSubItemTreeAdded
;
1048 event
.u
.input_item_subitem_tree_added
.p_root
= p_root
;
1049 vlc_event_send( &p_root
->p_item
->event_manager
, &event
);
1051 input_item_node_Delete( p_root
);
1054 /* Called by es_out when a new Elementary Stream is added or updated. */
1055 void input_item_UpdateTracksInfo(input_item_t
*item
, const es_format_t
*fmt
)
1058 es_format_t
*fmt_copy
= malloc(sizeof *fmt_copy
);
1062 es_format_Copy(fmt_copy
, fmt
);
1063 /* XXX: we could free p_extra to save memory, we will likely not need
1064 * the decoder specific data */
1066 vlc_mutex_lock( &item
->lock
);
1068 for( i
= 0; i
< item
->i_es
; i
++ )
1070 if (item
->es
[i
]->i_id
!= fmt
->i_id
)
1073 /* We've found the right ES, replace it */
1074 es_format_Clean(item
->es
[i
]);
1076 item
->es
[i
] = fmt_copy
;
1077 vlc_mutex_unlock( &item
->lock
);
1081 /* ES not found, insert it */
1082 TAB_APPEND(item
->i_es
, item
->es
, fmt_copy
);
1083 vlc_mutex_unlock( &item
->lock
);