]> git.sesse.net Git - vlc/blob - src/input/item.c
d74ec6689e3a195c69ff56bf7caa95ebcc0125b9
[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 void GuessType( 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
51     p_i->i_type = ITEM_TYPE_UNKNOWN;
52     p_i->b_fixed_name = true;
53
54     p_i->p_stats = NULL;
55     p_i->p_meta = NULL;
56
57     vlc_mutex_init( &p_i->lock );
58     vlc_event_manager_t * p_em = &p_i->event_manager;
59     vlc_event_manager_init( p_em, p_i, p_o );
60     vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
61     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
62     vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
63     vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
64     vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
65     vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
66     vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
67 }
68
69 static inline void input_item_Clean( input_item_t *p_i )
70 {
71     int i;
72
73     vlc_event_manager_fini( &p_i->event_manager );
74
75     free( p_i->psz_name );
76     free( p_i->psz_uri );
77     if( p_i->p_stats )
78     {
79         vlc_mutex_destroy( &p_i->p_stats->lock );
80         free( p_i->p_stats );
81     }
82
83     if( p_i->p_meta )
84         vlc_meta_Delete( p_i->p_meta );
85
86     for( i = 0; i < p_i->i_options; i++ )
87         free( p_i->ppsz_options[i] );
88     TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
89     free( p_i->optflagv);
90
91     for( i = 0; i < p_i->i_es; i++ )
92     {
93         es_format_Clean( p_i->es[i] );
94         free( p_i->es[i] );
95     }
96     TAB_CLEAN( p_i->i_es, p_i->es );
97
98     for( i = 0; i < p_i->i_categories; i++ )
99     {
100         info_category_t *p_category = p_i->pp_categories[i];
101         int j;
102
103         for( j = 0; j < p_category->i_infos; j++ )
104         {
105             struct info_t *p_info = p_category->pp_infos[j];
106
107             free( p_info->psz_name);
108             free( p_info->psz_value );
109             free( p_info );
110         }
111         TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
112
113         free( p_category->psz_name );
114         free( p_category );
115     }
116     TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
117
118     vlc_mutex_destroy( &p_i->lock );
119 }
120 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
121 {
122     bool b_changed;
123
124     vlc_mutex_lock( &p_i->lock );
125
126     b_changed = p_i->b_error_when_reading != b_error;
127     p_i->b_error_when_reading = b_error;
128
129     vlc_mutex_unlock( &p_i->lock );
130
131     if( b_changed )
132     {
133         vlc_event_t event;
134
135         event.type = vlc_InputItemErrorWhenReadingChanged;
136         event.u.input_item_error_when_reading_changed.new_value = b_error;
137         vlc_event_send( &p_i->event_manager, &event );
138     }
139 }
140 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
141 {
142     bool b_send_event = false;
143
144     vlc_mutex_lock( &p_i->lock );
145
146     if( !p_i->p_meta )
147         p_i->p_meta = vlc_meta_New();
148
149     int i_new_status;
150     if( b_preparsed )
151         i_new_status = p_i->p_meta->i_status | ITEM_PREPARSED;
152     else
153         i_new_status = p_i->p_meta->i_status & ~ITEM_PREPARSED;
154     if( p_i->p_meta->i_status != i_new_status )
155     {
156         p_i->p_meta->i_status = i_new_status;
157         b_send_event = true;
158     }
159
160     vlc_mutex_unlock( &p_i->lock );
161
162     if( b_send_event )
163     {
164         vlc_event_t event;
165         event.type = vlc_InputItemPreparsedChanged;
166         event.u.input_item_preparsed_changed.new_status = i_new_status;
167         vlc_event_send( &p_i->event_manager, &event );
168     }
169 }
170 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
171 {
172     vlc_mutex_lock( &p_i->lock );
173
174     if( !p_i->p_meta )
175         p_i->p_meta = vlc_meta_New();
176
177     if( b_not_found )
178         p_i->p_meta->i_status |= ITEM_ART_NOTFOUND;
179     else
180         p_i->p_meta->i_status &= ~ITEM_ART_NOTFOUND;
181
182     vlc_mutex_unlock( &p_i->lock );
183 }
184 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
185 {
186     vlc_mutex_lock( &p_i->lock );
187
188     if( !p_i->p_meta )
189         p_i->p_meta = vlc_meta_New();
190
191     if( b_art_fetched )
192         p_i->p_meta->i_status |= ITEM_ART_FETCHED;
193     else
194         p_i->p_meta->i_status &= ~ITEM_ART_FETCHED;
195
196     vlc_mutex_unlock( &p_i->lock );
197 }
198
199 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
200 {
201     vlc_event_t event;
202
203     vlc_mutex_lock( &p_i->lock );
204     if( !p_i->p_meta )
205         p_i->p_meta = vlc_meta_New();
206     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
207     vlc_mutex_unlock( &p_i->lock );
208
209     /* Notify interested third parties */
210     event.type = vlc_InputItemMetaChanged;
211     event.u.input_item_meta_changed.meta_type = meta_type;
212     vlc_event_send( &p_i->event_manager, &event );
213 }
214
215 /* FIXME GRRRRRRRRRR args should be in the reverse order to be 
216  * consistant with (nearly?) all or copy funcs */
217 void input_item_CopyOptions( input_item_t *p_parent,
218                              input_item_t *p_child )
219 {
220     vlc_mutex_lock( &p_parent->lock );
221
222     for( int i = 0 ; i< p_parent->i_options; i++ )
223     {
224         if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
225             continue;
226
227         input_item_AddOption( p_child,
228                               p_parent->ppsz_options[i],
229                               p_parent->optflagv[i] );
230     }
231
232     vlc_mutex_unlock( &p_parent->lock );
233 }
234
235 /* This won't hold the item, but can tell to interested third parties
236  * Like the playlist, that there is a new sub item. With this design
237  * It is not the input item's responsability to keep all the ref of
238  * the input item children. */
239 void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
240 {
241     vlc_mutex_lock( &p_parent->lock );
242
243     p_parent->i_type = ITEM_TYPE_PLAYLIST;
244
245     vlc_mutex_unlock( &p_parent->lock );
246
247     /* Notify interested third parties */
248     vlc_event_t event;
249
250     event.type = vlc_InputItemSubItemAdded;
251     event.u.input_item_subitem_added.p_new_child = p_child;
252     vlc_event_send( &p_parent->event_manager, &event );
253 }
254
255 bool input_item_HasErrorWhenReading( input_item_t *p_item )
256 {
257     vlc_mutex_lock( &p_item->lock );
258
259     bool b_error = p_item->b_error_when_reading;
260
261     vlc_mutex_unlock( &p_item->lock );
262
263     return b_error;
264 }
265
266 bool input_item_MetaMatch( input_item_t *p_i,
267                            vlc_meta_type_t meta_type, const char *psz )
268 {
269     vlc_mutex_lock( &p_i->lock );
270
271     if( !p_i->p_meta )
272     {
273         vlc_mutex_unlock( &p_i->lock );
274         return false;
275     }
276     const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
277     bool b_ret = psz_meta && strcasestr( psz_meta, psz );
278
279     vlc_mutex_unlock( &p_i->lock );
280
281     return b_ret;
282 }
283
284 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
285 {
286     vlc_mutex_lock( &p_i->lock );
287
288     if( !p_i->p_meta )
289     {
290         vlc_mutex_unlock( &p_i->lock );
291         return NULL;
292     }
293
294     char *psz = NULL;
295     if( vlc_meta_Get( p_i->p_meta, meta_type ) )
296         psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
297
298     vlc_mutex_unlock( &p_i->lock );
299     return psz;
300 }
301
302 /* Get the title of a given item or fallback to the name if the title is empty */
303 char *input_item_GetTitleFbName( input_item_t *p_item )
304 {
305     char *psz_ret;
306     vlc_mutex_lock( &p_item->lock );
307
308     if( !p_item->p_meta )
309     {
310         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
311         vlc_mutex_unlock( &p_item->lock );
312         return psz_ret;
313     }
314
315     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
316     if( !EMPTY_STR( psz_title ) )
317         psz_ret = strdup( psz_title );
318     else
319         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
320
321     vlc_mutex_unlock( &p_item->lock );
322     return psz_ret;
323 }
324
325 char *input_item_GetName( input_item_t *p_item )
326 {
327     vlc_mutex_lock( &p_item->lock );
328
329     char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
330
331     vlc_mutex_unlock( &p_item->lock );
332     return psz_name;
333 }
334 void input_item_SetName( input_item_t *p_item, const char *psz_name )
335 {
336     vlc_mutex_lock( &p_item->lock );
337
338     free( p_item->psz_name );
339     p_item->psz_name = strdup( psz_name );
340
341     vlc_mutex_unlock( &p_item->lock );
342 }
343
344 char *input_item_GetURI( input_item_t *p_i )
345 {
346     vlc_mutex_lock( &p_i->lock );
347
348     char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
349
350     vlc_mutex_unlock( &p_i->lock );
351     return psz_s;
352 }
353 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
354 {
355     vlc_mutex_lock( &p_i->lock );
356
357     free( p_i->psz_uri );
358     p_i->psz_uri = strdup( psz_uri );
359
360     GuessType( p_i );
361
362     if( !p_i->psz_name && p_i->i_type == ITEM_TYPE_FILE )
363     {
364         const char *psz_filename = strrchr( p_i->psz_uri, DIR_SEP_CHAR );
365         if( psz_filename && *psz_filename == DIR_SEP_CHAR )
366             psz_filename++;
367         if( psz_filename && *psz_filename )
368             p_i->psz_name = strdup( psz_filename );
369
370         /* Make the name more readable */
371         if( p_i->psz_name )
372             decode_URI( p_i->psz_name );
373     }
374
375     /* The name is NULL: fill it with everything except login and password */
376     if( !p_i->psz_name )
377     {
378         int r;
379         vlc_url_t url;
380         vlc_UrlParse( &url, psz_uri, 0 );
381         if( url.psz_protocol )
382         {
383             if( url.i_port > 0 )
384                 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
385                           url.psz_host, url.i_port,
386                           url.psz_path ? url.psz_path : "" );
387             else
388                 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
389                           url.psz_host ? url.psz_host : "",
390                           url.psz_path ? url.psz_path : "" );
391         }
392         else
393         {
394             if( url.i_port > 0 )
395                 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
396                           url.psz_path ? url.psz_path : "" );
397             else
398                 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
399                           url.psz_path ? url.psz_path : "" );
400         }
401         vlc_UrlClean( &url );
402         if( -1==r )
403             p_i->psz_name=NULL; /* recover from undefined value */
404
405         /* Make the name more readable */
406         if( p_i->psz_name )
407             decode_URI( p_i->psz_name );
408     }
409
410     vlc_mutex_unlock( &p_i->lock );
411 }
412
413 mtime_t input_item_GetDuration( input_item_t *p_i )
414 {
415     vlc_mutex_lock( &p_i->lock );
416
417     mtime_t i_duration = p_i->i_duration;
418
419     vlc_mutex_unlock( &p_i->lock );
420     return i_duration;
421 }
422
423 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
424 {
425     bool b_send_event = false;
426
427     vlc_mutex_lock( &p_i->lock );
428     if( p_i->i_duration != i_duration )
429     {
430         p_i->i_duration = i_duration;
431         b_send_event = true;
432     }
433     vlc_mutex_unlock( &p_i->lock );
434
435     if( b_send_event )
436     {
437         vlc_event_t event;
438
439         event.type = vlc_InputItemDurationChanged;
440         event.u.input_item_duration_changed.new_duration = i_duration;
441         vlc_event_send( &p_i->event_manager, &event );
442     }
443 }
444
445
446 bool input_item_IsPreparsed( input_item_t *p_item )
447 {
448     vlc_mutex_lock( &p_item->lock );
449     bool b_preparsed = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_PREPARSED ) != 0 : false;
450     vlc_mutex_unlock( &p_item->lock );
451
452     return b_preparsed;
453 }
454
455 bool input_item_IsArtFetched( input_item_t *p_item )
456 {
457     vlc_mutex_lock( &p_item->lock );
458     bool b_fetched = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_ART_FETCHED ) != 0 : false;
459     vlc_mutex_unlock( &p_item->lock );
460
461     return b_fetched;
462 }
463
464 static void input_item_Destroy ( gc_object_t *p_gc )
465 {
466     input_item_t *p_item = vlc_priv( p_gc, input_item_t );
467
468     input_item_Clean( p_item );
469     free( p_item );
470 }
471
472 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
473                           unsigned flags )
474 {
475     int err = VLC_SUCCESS;
476
477     if( psz_option == NULL )
478         return VLC_EGENERIC;
479
480     vlc_mutex_lock( &p_input->lock );
481     if (flags & VLC_INPUT_OPTION_UNIQUE)
482     {
483         for (int i = 0 ; i < p_input->i_options; i++)
484             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
485                 goto out;
486     }
487
488     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
489     if (flagv == NULL)
490     {
491         err = VLC_ENOMEM;
492         goto out;
493     }
494     p_input->optflagv = flagv;
495     flagv[p_input->optflagc++] = flags;
496
497     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
498                  p_input->i_options, strdup( psz_option ) );
499 out:
500     vlc_mutex_unlock( &p_input->lock );
501     return err;
502 }
503
504 /**
505  * Get a info item from a given category in a given input item.
506  *
507  * \param p_i The input item to get info from
508  * \param psz_cat String representing the category for the info
509  * \param psz_name String representing the name of the desired info
510  * \return A pointer to the string with the given info if found, or an
511  *         empty string otherwise. The caller should free the returned
512  *         pointer.
513  */
514 char *input_item_GetInfo( input_item_t *p_i,
515                           const char *psz_cat,
516                           const char *psz_name )
517 {
518     vlc_mutex_lock( &p_i->lock );
519
520     for( int i = 0; i< p_i->i_categories; i++ )
521     {
522         const info_category_t *p_cat = p_i->pp_categories[i];
523
524         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
525             continue;
526
527         for( int j = 0; j < p_cat->i_infos; j++ )
528         {
529             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
530             {
531                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
532                 vlc_mutex_unlock( &p_i->lock );
533                 return psz_ret;
534             }
535         }
536     }
537     vlc_mutex_unlock( &p_i->lock );
538     return strdup( "" );
539 }
540
541 static int InputItemVaAddInfo( input_item_t *p_i,
542                                const char *psz_cat,
543                                const char *psz_name,
544                                const char *psz_format, va_list args )
545 {
546     int i;
547     info_t *p_info = NULL;
548     info_category_t *p_cat = NULL ;
549
550     vlc_assert_locked( &p_i->lock );
551
552     for( i = 0 ; i < p_i->i_categories ; i ++ )
553     {
554         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
555         {
556             p_cat = p_i->pp_categories[i];
557             break;
558         }
559     }
560     if( !p_cat )
561     {
562         if( !(p_cat = malloc( sizeof(*p_cat) )) )
563             return VLC_ENOMEM;
564
565         p_cat->psz_name = strdup( psz_cat );
566         p_cat->i_infos = 0;
567         p_cat->pp_infos = 0;
568         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
569                      p_cat );
570     }
571
572     for( i = 0; i< p_cat->i_infos; i++ )
573     {
574         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
575         {
576             p_info = p_cat->pp_infos[i];
577             break;
578         }
579     }
580
581     if( !p_info )
582     {
583         if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
584             return VLC_ENOMEM;
585
586         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
587         p_info->psz_name = strdup( psz_name );
588     }
589     else
590     {
591         free( p_info->psz_value );
592     }
593
594     if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
595         p_info->psz_value = NULL;
596
597     return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
598 }
599
600 static int InputItemAddInfo( input_item_t *p_i,
601                              const char *psz_cat,
602                              const char *psz_name,
603                              const char *psz_format, ... )
604 {
605     va_list args;
606
607     va_start( args, psz_format );
608     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
609     va_end( args );
610
611     return i_ret;
612 }
613
614 int input_item_AddInfo( input_item_t *p_i,
615                         const char *psz_cat,
616                         const char *psz_name,
617                         const char *psz_format, ... )
618 {
619     va_list args;
620
621     vlc_mutex_lock( &p_i->lock );
622
623     va_start( args, psz_format );
624     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
625     va_end( args );
626
627     vlc_mutex_unlock( &p_i->lock );
628
629
630     if( !i_ret )
631     {
632         vlc_event_t event;
633
634         event.type = vlc_InputItemInfoChanged;
635         vlc_event_send( &p_i->event_manager, &event );
636     }
637     return i_ret;
638 }
639
640 int input_item_DelInfo( input_item_t *p_i,
641                         const char *psz_cat,
642                         const char *psz_name )
643 {
644     info_category_t *p_cat = NULL;
645     int i_cat;
646     int i;
647
648     vlc_mutex_lock( &p_i->lock );
649     for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
650     {
651         if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
652                      psz_cat ) )
653         {
654             p_cat = p_i->pp_categories[i_cat];
655             break;
656         }
657     }
658     if( p_cat == NULL )
659     {
660         vlc_mutex_unlock( &p_i->lock );
661         return VLC_EGENERIC;
662     }
663
664     if( psz_name )
665     {
666         /* Remove a specific info */
667         for( i = 0; i < p_cat->i_infos; i++ )
668         {
669             if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
670             {
671                 free( p_cat->pp_infos[i]->psz_name );
672                 free( p_cat->pp_infos[i]->psz_value );
673                 free( p_cat->pp_infos[i] );
674                 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
675                 break;
676             }
677         }
678         if( i >= p_cat->i_infos )
679         {
680             vlc_mutex_unlock( &p_i->lock );
681             return VLC_EGENERIC;
682         }
683     }
684     else
685     {
686         /* Remove the complete categorie */
687         for( i = 0; i < p_cat->i_infos; i++ )
688         {
689             free( p_cat->pp_infos[i]->psz_name );
690             free( p_cat->pp_infos[i]->psz_value );
691             free( p_cat->pp_infos[i] );
692         }
693         free( p_cat->pp_infos );
694         REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
695     }
696
697     // Free the category
698     free( p_cat->psz_name );
699     free( p_cat );
700     vlc_mutex_unlock( &p_i->lock );
701
702
703     vlc_event_t event;
704     event.type = vlc_InputItemInfoChanged;
705     vlc_event_send( &p_i->event_manager, &event );
706
707     return VLC_SUCCESS;
708 }
709
710 void input_item_SetEpg( input_item_t *p_item,
711                         const char *psz_epg, const vlc_epg_t *p_epg )
712 {
713     input_item_DelInfo( p_item, psz_epg, NULL );
714
715 #ifdef HAVE_LOCALTIME_R
716     vlc_mutex_lock( &p_item->lock );
717     for( int i = 0; i < p_epg->i_event; i++ )
718     {
719         const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
720         time_t t_start = (time_t)p_evt->i_start;
721         struct tm tm_start;
722         char psz_start[128];
723
724         localtime_r( &t_start, &tm_start );
725
726         snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
727                   1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
728                   tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
729         if( p_evt->psz_short_description || p_evt->psz_description )
730             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
731                               p_evt->psz_name,
732                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
733                               p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
734         else
735             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
736                               p_evt->psz_name,
737                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
738     }
739     vlc_mutex_unlock( &p_item->lock );
740
741     if( p_epg->i_event > 0 )
742     {
743         vlc_event_t event;
744
745         event.type = vlc_InputItemInfoChanged;
746         vlc_event_send( &p_item->event_manager, &event );
747     }
748 #endif
749 }
750
751
752 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
753                                   const char *psz_name,
754                                   int i_options,
755                                   const char *const *ppsz_options,
756                                   unsigned i_option_flags,
757                                   mtime_t i_duration )
758 {
759     return input_item_NewWithType( p_obj, psz_uri, psz_name,
760                                   i_options, ppsz_options, i_option_flags,
761                                   i_duration, ITEM_TYPE_UNKNOWN );
762 }
763
764
765 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
766                                 const char *psz_name,
767                                 int i_options,
768                                 const char *const *ppsz_options,
769                                 unsigned i_option_flags,
770                                 mtime_t i_duration,
771                                 int i_type )
772 {
773     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
774     static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
775
776     input_item_t* p_input = malloc( sizeof(input_item_t ) );
777     if( !p_input )
778         return NULL;
779
780     input_item_Init( p_obj, p_input );
781     vlc_gc_init( p_input, input_item_Destroy );
782
783     vlc_mutex_lock( &input_id_lock );
784     p_input->i_id = ++priv->i_last_input_id;
785     vlc_mutex_unlock( &input_id_lock );
786
787     p_input->b_fixed_name = false;
788
789     p_input->i_type = i_type;
790     p_input->b_prefers_tree = false;
791
792     if( psz_uri )
793         input_item_SetURI( p_input, psz_uri );
794
795     if( i_type != ITEM_TYPE_UNKNOWN )
796         p_input->i_type = i_type;
797
798     if( psz_name )
799         input_item_SetName( p_input, psz_name );
800
801     p_input->i_duration = i_duration;
802
803     for( int i = 0; i < i_options; i++ )
804         input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
805     return p_input;
806 }
807
808 /* Guess the type of the item using the beginning of the mrl */
809 static void GuessType( input_item_t *p_item)
810 {
811     int i;
812     static struct { const char *psz_search; int i_type; }  types_array[] =
813     {
814         { "http", ITEM_TYPE_NET },
815         { "dvd", ITEM_TYPE_DISC },
816         { "cdda", ITEM_TYPE_CDDA },
817         { "mms", ITEM_TYPE_NET },
818         { "rtsp", ITEM_TYPE_NET },
819         { "udp", ITEM_TYPE_NET },
820         { "rtp", ITEM_TYPE_NET },
821         { "vcd", ITEM_TYPE_DISC },
822         { "v4l", ITEM_TYPE_CARD },
823         { "dshow", ITEM_TYPE_CARD },
824         { "pvr", ITEM_TYPE_CARD },
825         { "dvb", ITEM_TYPE_CARD },
826         { "qpsk", ITEM_TYPE_CARD },
827         { "sdp", ITEM_TYPE_NET },
828         { "ftp", ITEM_TYPE_NET },
829         { "smb", ITEM_TYPE_NET },
830         { NULL, 0 }
831     };
832
833     if( !p_item->psz_uri || !strstr( p_item->psz_uri, "://" ) )
834     {
835         p_item->i_type = ITEM_TYPE_FILE;
836         return;
837     }
838
839     for( i = 0; types_array[i].psz_search != NULL; i++ )
840     {
841         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
842                       strlen( types_array[i].psz_search ) ) )
843         {
844             p_item->i_type = types_array[i].i_type;
845             return;
846         }
847     }
848     p_item->i_type = ITEM_TYPE_FILE;
849 }