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