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