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