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