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