]> git.sesse.net Git - vlc/blob - src/input/item.c
3ce82c63dba832cbeb0c8ee363ed44799b31fe50
[vlc] / src / input / item.c
1 /*****************************************************************************
2  * item.c: input_item management
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_url.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
33
34 #include "item.h"
35 #include "info.h"
36
37 static int GuessType( const input_item_t *p_item );
38
39 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
40  * anyway. */
41 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
42 {
43     memset( p_i, 0, sizeof(input_item_t) );
44
45     p_i->psz_name = NULL;
46     p_i->psz_uri = NULL;
47     TAB_INIT( p_i->i_es, p_i->es );
48     TAB_INIT( p_i->i_options, p_i->ppsz_options );
49     p_i->optflagv = NULL, p_i->optflagc = 0;
50     TAB_INIT( p_i->i_categories, p_i->pp_categories );
51     TAB_INIT( p_i->i_epg, p_i->pp_epg );
52
53     p_i->i_type = ITEM_TYPE_UNKNOWN;
54     p_i->b_fixed_name = true;
55
56     p_i->p_stats = NULL;
57     p_i->p_meta = NULL;
58
59     vlc_mutex_init( &p_i->lock );
60     vlc_event_manager_t * p_em = &p_i->event_manager;
61     vlc_event_manager_init( p_em, p_i, p_o );
62     vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
63     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
64     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
65     vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
66     vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
67     vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
68     vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
69     vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
70 }
71
72 static inline void input_item_Clean( input_item_t *p_i )
73 {
74     int i;
75
76     vlc_event_manager_fini( &p_i->event_manager );
77
78     free( p_i->psz_name );
79     free( p_i->psz_uri );
80     if( p_i->p_stats )
81     {
82         vlc_mutex_destroy( &p_i->p_stats->lock );
83         free( p_i->p_stats );
84     }
85
86     if( p_i->p_meta )
87         vlc_meta_Delete( p_i->p_meta );
88
89     for( i = 0; i < p_i->i_options; i++ )
90         free( p_i->ppsz_options[i] );
91     TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
92     free( p_i->optflagv);
93
94     for( i = 0; i < p_i->i_es; i++ )
95     {
96         es_format_Clean( p_i->es[i] );
97         free( p_i->es[i] );
98     }
99     TAB_CLEAN( p_i->i_es, p_i->es );
100
101     for( i = 0; i < p_i->i_epg; i++ )
102         vlc_epg_Delete( p_i->pp_epg[i] );
103     TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
104
105     for( i = 0; i < p_i->i_categories; i++ )
106         info_category_Delete( p_i->pp_categories[i] );
107     TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
108
109     vlc_mutex_destroy( &p_i->lock );
110 }
111 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
112 {
113     bool b_changed;
114
115     vlc_mutex_lock( &p_i->lock );
116
117     b_changed = p_i->b_error_when_reading != b_error;
118     p_i->b_error_when_reading = b_error;
119
120     vlc_mutex_unlock( &p_i->lock );
121
122     if( b_changed )
123     {
124         vlc_event_t event;
125
126         event.type = vlc_InputItemErrorWhenReadingChanged;
127         event.u.input_item_error_when_reading_changed.new_value = b_error;
128         vlc_event_send( &p_i->event_manager, &event );
129     }
130 }
131 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
132 {
133     bool b_send_event = false;
134
135     vlc_mutex_lock( &p_i->lock );
136
137     if( !p_i->p_meta )
138         p_i->p_meta = vlc_meta_New();
139
140     int status = vlc_meta_GetStatus(p_i->p_meta);
141     int new_status;
142     if( b_preparsed )
143         new_status = status | ITEM_PREPARSED;
144     else
145         new_status = status & ~ITEM_PREPARSED;
146     if( status != new_status )
147     {
148         vlc_meta_SetStatus(p_i->p_meta, new_status);
149         b_send_event = true;
150     }
151
152     vlc_mutex_unlock( &p_i->lock );
153
154     if( b_send_event )
155     {
156         vlc_event_t event;
157         event.type = vlc_InputItemPreparsedChanged;
158         event.u.input_item_preparsed_changed.new_status = new_status;
159         vlc_event_send( &p_i->event_manager, &event );
160     }
161 }
162
163 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
164 {
165     vlc_mutex_lock( &p_i->lock );
166
167     if( !p_i->p_meta )
168         p_i->p_meta = vlc_meta_New();
169
170     int status = vlc_meta_GetStatus(p_i->p_meta);
171
172     if( b_not_found )
173         status |= ITEM_ART_NOTFOUND;
174     else
175         status &= ~ITEM_ART_NOTFOUND;
176
177     vlc_meta_SetStatus(p_i->p_meta, status);
178
179     vlc_mutex_unlock( &p_i->lock );
180 }
181
182 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
183 {
184     vlc_mutex_lock( &p_i->lock );
185
186     if( !p_i->p_meta )
187         p_i->p_meta = vlc_meta_New();
188
189     int status = vlc_meta_GetStatus(p_i->p_meta);
190
191     if( b_art_fetched )
192         status |= ITEM_ART_FETCHED;
193     else
194         status &= ~ITEM_ART_FETCHED;
195
196     vlc_meta_SetStatus(p_i->p_meta, status);
197
198     vlc_mutex_unlock( &p_i->lock );
199 }
200
201 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
202 {
203     vlc_event_t event;
204
205     vlc_mutex_lock( &p_i->lock );
206     if( !p_i->p_meta )
207         p_i->p_meta = vlc_meta_New();
208     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
209     vlc_mutex_unlock( &p_i->lock );
210
211     /* Notify interested third parties */
212     event.type = vlc_InputItemMetaChanged;
213     event.u.input_item_meta_changed.meta_type = meta_type;
214     vlc_event_send( &p_i->event_manager, &event );
215 }
216
217 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
218  * consistant with (nearly?) all or copy funcs */
219 void input_item_CopyOptions( input_item_t *p_parent,
220                              input_item_t *p_child )
221 {
222     vlc_mutex_lock( &p_parent->lock );
223
224     for( int i = 0 ; i< p_parent->i_options; i++ )
225     {
226         if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
227             continue;
228
229         input_item_AddOption( p_child,
230                               p_parent->ppsz_options[i],
231                               p_parent->optflagv[i] );
232     }
233
234     vlc_mutex_unlock( &p_parent->lock );
235 }
236
237 static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
238 {
239     /* Notify interested third parties */
240     vlc_event_t event;
241     event.type = vlc_InputItemSubItemAdded;
242     event.u.input_item_subitem_added.p_new_child = p_child;
243     vlc_event_send( &p_parent->event_manager, &event );
244 }
245
246 /* This won't hold the item, but can tell to interested third parties
247  * Like the playlist, that there is a new sub item. With this design
248  * It is not the input item's responsability to keep all the ref of
249  * the input item children. */
250 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
251 {
252     vlc_mutex_lock( &p_parent->lock );
253     p_parent->i_type = ITEM_TYPE_PLAYLIST;
254     vlc_mutex_unlock( &p_parent->lock );
255
256     input_item_node_t *p_node = input_item_node_Create( p_parent );
257     input_item_node_AppendItem( p_node, p_child );
258     input_item_node_PostAndDelete( p_node );
259 }
260
261 bool input_item_HasErrorWhenReading( input_item_t *p_item )
262 {
263     vlc_mutex_lock( &p_item->lock );
264
265     bool b_error = p_item->b_error_when_reading;
266
267     vlc_mutex_unlock( &p_item->lock );
268
269     return b_error;
270 }
271
272 bool input_item_MetaMatch( input_item_t *p_i,
273                            vlc_meta_type_t meta_type, const char *psz )
274 {
275     vlc_mutex_lock( &p_i->lock );
276
277     if( !p_i->p_meta )
278     {
279         vlc_mutex_unlock( &p_i->lock );
280         return false;
281     }
282     const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
283     bool b_ret = psz_meta && strcasestr( psz_meta, psz );
284
285     vlc_mutex_unlock( &p_i->lock );
286
287     return b_ret;
288 }
289
290 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
291 {
292     vlc_mutex_lock( &p_i->lock );
293
294     if( !p_i->p_meta )
295     {
296         vlc_mutex_unlock( &p_i->lock );
297         return NULL;
298     }
299
300     char *psz = NULL;
301     if( vlc_meta_Get( p_i->p_meta, meta_type ) )
302         psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
303
304     vlc_mutex_unlock( &p_i->lock );
305     return psz;
306 }
307
308 /* Get the title of a given item or fallback to the name if the title is empty */
309 char *input_item_GetTitleFbName( input_item_t *p_item )
310 {
311     char *psz_ret;
312     vlc_mutex_lock( &p_item->lock );
313
314     if( !p_item->p_meta )
315     {
316         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
317         vlc_mutex_unlock( &p_item->lock );
318         return psz_ret;
319     }
320
321     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
322     if( !EMPTY_STR( psz_title ) )
323         psz_ret = strdup( psz_title );
324     else
325         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
326
327     vlc_mutex_unlock( &p_item->lock );
328     return psz_ret;
329 }
330
331 char *input_item_GetName( input_item_t *p_item )
332 {
333     vlc_mutex_lock( &p_item->lock );
334
335     char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
336
337     vlc_mutex_unlock( &p_item->lock );
338     return psz_name;
339 }
340 void input_item_SetName( input_item_t *p_item, const char *psz_name )
341 {
342     vlc_mutex_lock( &p_item->lock );
343
344     free( p_item->psz_name );
345     p_item->psz_name = strdup( psz_name );
346
347     vlc_mutex_unlock( &p_item->lock );
348 }
349
350 char *input_item_GetURI( input_item_t *p_i )
351 {
352     vlc_mutex_lock( &p_i->lock );
353
354     char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
355
356     vlc_mutex_unlock( &p_i->lock );
357     return psz_s;
358 }
359 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
360 {
361     vlc_mutex_lock( &p_i->lock );
362 #ifndef NDEBUG
363     if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
364         fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
365 #endif
366
367     free( p_i->psz_uri );
368     p_i->psz_uri = strdup( psz_uri );
369
370     p_i->i_type = GuessType( p_i );
371
372     if( p_i->psz_name )
373         ;
374     else
375     if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
376     {
377         const char *psz_filename = strrchr( p_i->psz_uri, '/' );
378
379         if( psz_filename && *psz_filename == '/' )
380             psz_filename++;
381         if( psz_filename && *psz_filename )
382             p_i->psz_name = strdup( psz_filename );
383
384         /* Make the name more readable */
385         if( p_i->psz_name )
386             decode_URI( p_i->psz_name );
387     }
388     else
389     {   /* Strip login and password from title */
390         int r;
391         vlc_url_t url;
392
393         vlc_UrlParse( &url, psz_uri, 0 );
394         if( url.psz_protocol )
395         {
396             if( url.i_port > 0 )
397                 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
398                           url.psz_host, url.i_port,
399                           url.psz_path ? url.psz_path : "" );
400             else
401                 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
402                           url.psz_host ? url.psz_host : "",
403                           url.psz_path ? url.psz_path : "" );
404         }
405         else
406         {
407             if( url.i_port > 0 )
408                 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
409                           url.psz_path ? url.psz_path : "" );
410             else
411                 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
412                           url.psz_path ? url.psz_path : "" );
413         }
414         vlc_UrlClean( &url );
415         if( -1==r )
416             p_i->psz_name=NULL; /* recover from undefined value */
417     }
418
419     vlc_mutex_unlock( &p_i->lock );
420 }
421
422 mtime_t input_item_GetDuration( input_item_t *p_i )
423 {
424     vlc_mutex_lock( &p_i->lock );
425
426     mtime_t i_duration = p_i->i_duration;
427
428     vlc_mutex_unlock( &p_i->lock );
429     return i_duration;
430 }
431
432 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
433 {
434     bool b_send_event = false;
435
436     vlc_mutex_lock( &p_i->lock );
437     if( p_i->i_duration != i_duration )
438     {
439         p_i->i_duration = i_duration;
440         b_send_event = true;
441     }
442     vlc_mutex_unlock( &p_i->lock );
443
444     if( b_send_event )
445     {
446         vlc_event_t event;
447
448         event.type = vlc_InputItemDurationChanged;
449         event.u.input_item_duration_changed.new_duration = i_duration;
450         vlc_event_send( &p_i->event_manager, &event );
451     }
452 }
453
454
455 bool input_item_IsPreparsed( input_item_t *p_item )
456 {
457     vlc_mutex_lock( &p_item->lock );
458     bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
459     vlc_mutex_unlock( &p_item->lock );
460
461     return b_preparsed;
462 }
463
464 bool input_item_IsArtFetched( input_item_t *p_item )
465 {
466     vlc_mutex_lock( &p_item->lock );
467     bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
468     vlc_mutex_unlock( &p_item->lock );
469
470     return b_fetched;
471 }
472
473 static void input_item_Destroy ( gc_object_t *p_gc )
474 {
475     input_item_t *p_item = vlc_priv( p_gc, input_item_t );
476
477     input_item_Clean( p_item );
478     free( p_item );
479 }
480
481 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
482                           unsigned flags )
483 {
484     int err = VLC_SUCCESS;
485
486     if( psz_option == NULL )
487         return VLC_EGENERIC;
488
489     vlc_mutex_lock( &p_input->lock );
490     if (flags & VLC_INPUT_OPTION_UNIQUE)
491     {
492         for (int i = 0 ; i < p_input->i_options; i++)
493             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
494                 goto out;
495     }
496
497     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
498     if (flagv == NULL)
499     {
500         err = VLC_ENOMEM;
501         goto out;
502     }
503     p_input->optflagv = flagv;
504     flagv[p_input->optflagc++] = flags;
505
506     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
507                  p_input->i_options, strdup( psz_option ) );
508 out:
509     vlc_mutex_unlock( &p_input->lock );
510     return err;
511 }
512
513 static info_category_t *InputItemFindCat( input_item_t *p_item,
514                                           int *pi_index, const char *psz_cat )
515 {
516     vlc_assert_locked( &p_item->lock );
517     for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
518     {
519         info_category_t *p_cat = p_item->pp_categories[i];
520
521         if( !strcmp( p_cat->psz_name, psz_cat ) )
522         {
523             if( pi_index )
524                 *pi_index = i;
525             return p_cat;
526         }
527     }
528     return NULL;
529 }
530
531 /**
532  * Get a info item from a given category in a given input item.
533  *
534  * \param p_i The input item to get info from
535  * \param psz_cat String representing the category for the info
536  * \param psz_name String representing the name of the desired info
537  * \return A pointer to the string with the given info if found, or an
538  *         empty string otherwise. The caller should free the returned
539  *         pointer.
540  */
541 char *input_item_GetInfo( input_item_t *p_i,
542                           const char *psz_cat,
543                           const char *psz_name )
544 {
545     vlc_mutex_lock( &p_i->lock );
546
547     const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
548     if( p_cat )
549     {
550         info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
551         if( p_info && p_info->psz_value )
552         {
553             char *psz_ret = strdup( p_info->psz_value );
554             vlc_mutex_unlock( &p_i->lock );
555             return psz_ret;
556         }
557     }
558     vlc_mutex_unlock( &p_i->lock );
559     return strdup( "" );
560 }
561
562 static int InputItemVaAddInfo( input_item_t *p_i,
563                                const char *psz_cat,
564                                const char *psz_name,
565                                const char *psz_format, va_list args )
566 {
567     vlc_assert_locked( &p_i->lock );
568
569     info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
570     if( !p_cat )
571     {
572         p_cat = info_category_New( psz_cat );
573         if( !p_cat )
574             return VLC_ENOMEM;
575         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
576                      p_cat );
577     }
578     info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
579     if( !p_info || !p_info->psz_value )
580         return VLC_EGENERIC;
581     return VLC_SUCCESS;
582 }
583
584 static int InputItemAddInfo( input_item_t *p_i,
585                              const char *psz_cat,
586                              const char *psz_name,
587                              const char *psz_format, ... )
588 {
589     va_list args;
590
591     va_start( args, psz_format );
592     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
593     va_end( args );
594
595     return i_ret;
596 }
597
598 int input_item_AddInfo( input_item_t *p_i,
599                         const char *psz_cat,
600                         const char *psz_name,
601                         const char *psz_format, ... )
602 {
603     va_list args;
604
605     vlc_mutex_lock( &p_i->lock );
606
607     va_start( args, psz_format );
608     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
609     va_end( args );
610
611     vlc_mutex_unlock( &p_i->lock );
612
613
614     if( !i_ret )
615     {
616         vlc_event_t event;
617
618         event.type = vlc_InputItemInfoChanged;
619         vlc_event_send( &p_i->event_manager, &event );
620     }
621     return i_ret;
622 }
623
624 int input_item_DelInfo( input_item_t *p_i,
625                         const char *psz_cat,
626                         const char *psz_name )
627 {
628
629     vlc_mutex_lock( &p_i->lock );
630     int i_cat;
631     info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
632     if( !p_cat )
633     {
634         vlc_mutex_unlock( &p_i->lock );
635         return VLC_EGENERIC;
636     }
637
638     if( psz_name )
639     {
640         /* Remove a specific info */
641         int i_ret = info_category_DeleteInfo( p_cat, psz_name );
642         if( i_ret )
643         {
644             vlc_mutex_unlock( &p_i->lock );
645             return VLC_EGENERIC;
646         }
647     }
648     else
649     {
650         /* Remove the complete categorie */
651         info_category_Delete( p_cat );
652         REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
653     }
654     vlc_mutex_unlock( &p_i->lock );
655
656
657     vlc_event_t event;
658     event.type = vlc_InputItemInfoChanged;
659     vlc_event_send( &p_i->event_manager, &event );
660
661     return VLC_SUCCESS;
662 }
663
664 #define EPG_DEBUG
665 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
666 {
667     vlc_mutex_lock( &p_item->lock );
668
669     /* */
670     vlc_epg_t *p_epg = NULL;
671     for( int i = 0; i < p_item->i_epg; i++ )
672     {
673         vlc_epg_t *p_tmp = p_item->pp_epg[i];
674
675         if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
676             continue;
677         if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
678             continue;
679
680         p_epg = p_tmp;
681         break;
682     }
683
684     /* */
685     if( !p_epg )
686     {
687         p_epg = vlc_epg_New( p_update->psz_name );
688         if( p_epg )
689             TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
690     }
691     if( p_epg )
692         vlc_epg_Merge( p_epg, p_update );
693
694     vlc_mutex_unlock( &p_item->lock );
695
696 #ifdef EPG_DEBUG
697     char *psz_epg;
698     if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
699         goto signal;
700
701     input_item_DelInfo( p_item, psz_epg, NULL );
702
703     vlc_mutex_lock( &p_item->lock );
704     for( int i = 0; i < p_epg->i_event; i++ )
705     {
706         const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
707         time_t t_start = (time_t)p_evt->i_start;
708         struct tm tm_start;
709         char psz_start[128];
710
711         localtime_r( &t_start, &tm_start );
712
713         snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
714                   1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
715                   tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
716         if( p_evt->psz_short_description || p_evt->psz_description )
717             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
718                               p_evt->psz_name,
719                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
720                               p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
721                               p_evt->psz_description ? p_evt->psz_description : "" );
722         else
723             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
724                               p_evt->psz_name,
725                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
726     }
727     vlc_mutex_unlock( &p_item->lock );
728     free( psz_epg );
729 signal:
730 #endif
731
732     if( p_epg->i_event > 0 )
733     {
734         vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
735         vlc_event_send( &p_item->event_manager, &event );
736     }
737 }
738
739 void input_item_SetEpgOffline( input_item_t *p_item )
740 {
741     vlc_mutex_lock( &p_item->lock );
742     for( int i = 0; i < p_item->i_epg; i++ )
743         vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
744     vlc_mutex_unlock( &p_item->lock );
745
746 #ifdef EPG_DEBUG
747     vlc_mutex_lock( &p_item->lock );
748     const int i_epg_info = p_item->i_epg;
749     char *ppsz_epg_info[i_epg_info];
750     for( int i = 0; i < p_item->i_epg; i++ )
751     {
752         const vlc_epg_t *p_epg = p_item->pp_epg[i];
753         if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
754             ppsz_epg_info[i] = NULL;
755     }
756     vlc_mutex_unlock( &p_item->lock );
757
758     for( int i = 0; i < i_epg_info; i++ )
759     {
760         if( !ppsz_epg_info[i] )
761             continue;
762         input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
763         free( ppsz_epg_info[i] );
764     }
765 #endif
766
767     vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
768     vlc_event_send( &p_item->event_manager, &event );
769 }
770
771
772 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
773                                   const char *psz_name,
774                                   int i_options,
775                                   const char *const *ppsz_options,
776                                   unsigned i_option_flags,
777                                   mtime_t i_duration )
778 {
779     return input_item_NewWithType( p_obj, psz_uri, psz_name,
780                                   i_options, ppsz_options, i_option_flags,
781                                   i_duration, ITEM_TYPE_UNKNOWN );
782 }
783
784
785 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
786                                 const char *psz_name,
787                                 int i_options,
788                                 const char *const *ppsz_options,
789                                 unsigned i_option_flags,
790                                 mtime_t i_duration,
791                                 int i_type )
792 {
793     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
794     static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
795
796     input_item_t* p_input = malloc( sizeof(input_item_t ) );
797     if( !p_input )
798         return NULL;
799
800     input_item_Init( p_obj, p_input );
801     vlc_gc_init( p_input, input_item_Destroy );
802
803     vlc_mutex_lock( &input_id_lock );
804     p_input->i_id = ++priv->i_last_input_id;
805     vlc_mutex_unlock( &input_id_lock );
806
807     p_input->b_fixed_name = false;
808
809     p_input->i_type = i_type;
810     p_input->b_prefers_tree = false;
811
812     if( psz_uri )
813         input_item_SetURI( p_input, psz_uri );
814
815     if( i_type != ITEM_TYPE_UNKNOWN )
816         p_input->i_type = i_type;
817
818     if( psz_name )
819         input_item_SetName( p_input, psz_name );
820
821     p_input->i_duration = i_duration;
822
823     for( int i = 0; i < i_options; i++ )
824         input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
825     return p_input;
826 }
827
828 struct item_type_entry
829 {
830     const char psz_scheme[7];
831     uint8_t    i_type;
832 };
833
834 static int typecmp( const void *key, const void *entry )
835 {
836     const struct item_type_entry *type = entry;
837     const char *uri = key, *scheme = type->psz_scheme;
838
839     return strncmp( uri, scheme, strlen( scheme ) );
840 }
841
842 /* Guess the type of the item using the beginning of the mrl */
843 static int GuessType( const input_item_t *p_item )
844 {
845     static const struct item_type_entry tab[] =
846     {   /* /!\ Alphabetical order /!\ */
847         /* Short match work, not just exact match */
848         { "alsa",   ITEM_TYPE_CARD },
849         { "atsc",   ITEM_TYPE_CARD },
850         { "bd",     ITEM_TYPE_DISC },
851         { "cable",  ITEM_TYPE_CARD },
852         { "cdda",   ITEM_TYPE_CDDA },
853         { "dc1394", ITEM_TYPE_CARD },
854         { "dccp",   ITEM_TYPE_NET },
855         { "dir",    ITEM_TYPE_DIRECTORY },
856         { "dshow",  ITEM_TYPE_CARD },
857         { "dv",     ITEM_TYPE_CARD },
858         { "dvb",    ITEM_TYPE_CARD },
859         { "dvd",    ITEM_TYPE_DISC },
860         { "ftp",    ITEM_TYPE_NET },
861         { "http",   ITEM_TYPE_NET },
862         { "icyx",   ITEM_TYPE_NET },
863         { "itpc",   ITEM_TYPE_NET },
864         { "jack",   ITEM_TYPE_CARD },
865         { "live",   ITEM_TYPE_NET }, /* livedotcom */
866         { "mms",    ITEM_TYPE_NET },
867         { "mtp",    ITEM_TYPE_DISC },
868         { "ofdm",   ITEM_TYPE_CARD },
869         { "oss",    ITEM_TYPE_CARD },
870         { "pnm",    ITEM_TYPE_NET },
871         { "pvr",    ITEM_TYPE_CARD },
872         { "qam",    ITEM_TYPE_CARD },
873         { "qpsk",   ITEM_TYPE_CARD },
874         { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
875         { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
876         { "rt",     ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
877         { "satell", ITEM_TYPE_CARD }, /* sattelite */
878         { "screen", ITEM_TYPE_CARD },
879         { "sdp",    ITEM_TYPE_NET },
880         { "smb",    ITEM_TYPE_NET },
881         { "svcd",   ITEM_TYPE_DISC },
882         { "tcp",    ITEM_TYPE_NET },
883         { "terres", ITEM_TYPE_CARD }, /* terrestrial */
884         { "udp",    ITEM_TYPE_NET },  /* udplite too */
885         { "unsv",   ITEM_TYPE_NET },
886         { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
887         { "v4l",    ITEM_TYPE_CARD },
888         { "vcd",    ITEM_TYPE_DISC },
889         { "window", ITEM_TYPE_CARD },
890     };
891     const struct item_type_entry *e;
892
893     if( !strstr( p_item->psz_uri, "://" ) )
894         return ITEM_TYPE_FILE;
895
896     e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
897                  sizeof( tab[0] ), typecmp );
898     return e ? e->i_type : ITEM_TYPE_FILE;
899 }
900
901 input_item_node_t *input_item_node_Create( input_item_t *p_input )
902 {
903     input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
904     if( !p_node )
905         return NULL;
906
907     assert( p_input );
908
909     p_node->p_item = p_input;
910     vlc_gc_incref( p_input );
911
912     p_node->p_parent = NULL;
913     p_node->i_children = 0;
914     p_node->pp_children = NULL;
915
916     return p_node;
917 }
918
919 static void RecursiveNodeDelete( input_item_node_t *p_node )
920 {
921   for( int i = 0; i < p_node->i_children; i++ )
922       RecursiveNodeDelete( p_node->pp_children[i] );
923
924   vlc_gc_decref( p_node->p_item );
925   free( p_node->pp_children );
926   free( p_node );
927 }
928
929 void input_item_node_Delete( input_item_node_t *p_node )
930 {
931   if(  p_node->p_parent )
932   {
933       for( int i = 0; i < p_node->p_parent->i_children; i++ )
934           if( p_node->p_parent->pp_children[i] == p_node )
935           {
936               REMOVE_ELEM( p_node->p_parent->pp_children,
937                            p_node->p_parent->i_children,
938                            i );
939               break;
940           }
941   }
942
943   RecursiveNodeDelete( p_node );
944 }
945
946 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
947 {
948     input_item_node_t *p_new_child = input_item_node_Create( p_item );
949     if( !p_new_child ) return NULL;
950     input_item_node_AppendNode( p_node, p_new_child );
951     return p_new_child;
952 }
953
954 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
955 {
956     notify_subitem_added(p_parent->p_item, p_child->p_item);
957
958     assert( p_parent && p_child && p_child->p_parent == NULL );
959     INSERT_ELEM( p_parent->pp_children,
960                  p_parent->i_children,
961                  p_parent->i_children,
962                  p_child );
963     p_child->p_parent = p_parent;
964 }
965
966 void input_item_node_PostAndDelete( input_item_node_t *p_root )
967 {
968   vlc_event_t event;
969   event.type = vlc_InputItemSubItemTreeAdded;
970   event.u.input_item_subitem_tree_added.p_root = p_root;
971   vlc_event_send( &p_root->p_item->event_manager, &event );
972
973   input_item_node_Delete( p_root );
974 }