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