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