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