]> git.sesse.net Git - vlc/blob - src/input/item.c
input: Expose input_ItemHasErrorWhenReading.
[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 "input_internal.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_ItemInit( vlc_object_t *p_o, input_item_t *p_i )
40 {
41     memset( p_i, 0, sizeof(input_item_t) );
42     p_i->psz_name = NULL;
43     p_i->psz_uri = NULL;
44     TAB_INIT( p_i->i_es, p_i->es );
45     TAB_INIT( p_i->i_options, p_i->ppsz_options );
46     p_i->optflagv = NULL, p_i->optflagc = 0;
47     TAB_INIT( p_i->i_categories, p_i->pp_categories );
48
49     p_i->i_type = ITEM_TYPE_UNKNOWN;
50     p_i->b_fixed_name = true;
51
52     p_i->p_stats = NULL;
53     p_i->p_meta = NULL;
54
55     vlc_mutex_init( &p_i->lock );
56     vlc_event_manager_t * p_em = &p_i->event_manager;
57     vlc_event_manager_init( p_em, p_i, p_o );
58     vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
59     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
60     vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
61     vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
62     vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
63     vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
64     vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
65 }
66
67 static inline void input_ItemClean( input_item_t *p_i )
68 {
69     int i;
70
71     vlc_event_manager_fini( &p_i->event_manager );
72
73     free( p_i->psz_name );
74     free( p_i->psz_uri );
75     if( p_i->p_stats )
76     {
77         vlc_mutex_destroy( &p_i->p_stats->lock );
78         free( p_i->p_stats );
79     }
80
81     if( p_i->p_meta )
82         vlc_meta_Delete( p_i->p_meta );
83
84     for( i = 0; i < p_i->i_options; i++ )
85         free( p_i->ppsz_options[i] );
86     TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
87     free( p_i->optflagv);
88
89     for( i = 0; i < p_i->i_es; i++ )
90     {
91         es_format_Clean( p_i->es[i] );
92         free( p_i->es[i] );
93     }
94     TAB_CLEAN( p_i->i_es, p_i->es );
95
96     for( i = 0; i < p_i->i_categories; i++ )
97     {
98         info_category_t *p_category = p_i->pp_categories[i];
99         int j;
100
101         for( j = 0; j < p_category->i_infos; j++ )
102         {
103             struct info_t *p_info = p_category->pp_infos[j];
104
105             free( p_info->psz_name);
106             free( p_info->psz_value );
107             free( p_info );
108         }
109         TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
110
111         free( p_category->psz_name );
112         free( p_category );
113     }
114     TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
115
116     vlc_mutex_destroy( &p_i->lock );
117 }
118
119 void input_item_SetHasErrorWhenReading( input_item_t *p_i, bool error )
120 {
121     vlc_event_t event;
122
123     if( p_i->b_error_when_reading == error )
124         return;
125
126     p_i->b_error_when_reading = error;
127
128     /* Notify interested third parties */
129     event.type = vlc_InputItemErrorWhenReadingChanged;
130     event.u.input_item_error_when_reading_changed.new_value = error;
131     vlc_event_send( &p_i->event_manager, &event );
132 }
133
134 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
135 {
136     vlc_event_t event;
137
138     vlc_mutex_lock( &p_i->lock );
139     if( !p_i->p_meta )
140         p_i->p_meta = vlc_meta_New();
141     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
142     vlc_mutex_unlock( &p_i->lock );
143
144     /* Notify interested third parties */
145     event.type = vlc_InputItemMetaChanged;
146     event.u.input_item_meta_changed.meta_type = meta_type;
147     vlc_event_send( &p_i->event_manager, &event );
148 }
149
150 /**
151  * Get the item from an input thread
152  */
153 input_item_t *input_GetItem( input_thread_t *p_input )
154 {
155     assert( p_input && p_input->p );
156     return p_input->p->input.p_item;
157 }
158
159 /**
160  * Get a info item from a given category in a given input item.
161  *
162  * \param p_i The input item to get info from
163  * \param psz_cat String representing the category for the info
164  * \param psz_name String representing the name of the desired info
165  * \return A pointer to the string with the given info if found, or an
166  *         empty string otherwise. The caller should free the returned
167  *         pointer.
168  */
169 char *input_ItemGetInfo( input_item_t *p_i,
170                               const char *psz_cat,
171                               const char *psz_name )
172 {
173     int i,j;
174
175     vlc_mutex_lock( &p_i->lock );
176
177     for( i = 0 ; i< p_i->i_categories  ; i++ )
178     {
179         info_category_t *p_cat = p_i->pp_categories[i];
180
181         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
182             continue;
183
184         for( j = 0; j < p_cat->i_infos ; j++ )
185         {
186             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
187             {
188                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
189                 vlc_mutex_unlock( &p_i->lock );
190                 return psz_ret;
191             }
192         }
193     }
194     vlc_mutex_unlock( &p_i->lock );
195     return strdup( "" );
196 }
197
198 static void input_ItemDestroy ( gc_object_t *p_this )
199 {
200     vlc_object_t *p_obj = (vlc_object_t *)p_this->p_destructor_arg;
201     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
202     input_item_t *p_input = (input_item_t *) p_this;
203     int i;
204
205     input_ItemClean( p_input );
206
207     vlc_object_lock( p_obj->p_libvlc );
208
209     ARRAY_BSEARCH( priv->input_items,->i_id, int, p_input->i_id, i);
210     if( i != -1 )
211         ARRAY_REMOVE( priv->input_items, i);
212
213     vlc_object_unlock( p_obj->p_libvlc );
214
215     free( p_input );
216 }
217
218 int input_ItemAddOpt( input_item_t *p_input, const char *psz_option,
219                       unsigned flags )
220 {
221     int err = VLC_SUCCESS;
222
223     if( psz_option == NULL )
224         return VLC_EGENERIC;
225
226     vlc_mutex_lock( &p_input->lock );
227     if (flags & VLC_INPUT_OPTION_UNIQUE)
228     {
229         for (int i = 0 ; i < p_input->i_options; i++)
230             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
231                 goto out;
232     }
233
234     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
235     if (flagv == NULL)
236     {
237         err = VLC_ENOMEM;
238         goto out;
239     }
240     p_input->optflagv = flagv;
241     flagv[p_input->optflagc++] = flags;
242
243     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
244                  p_input->i_options, strdup( psz_option ) );
245 out:
246     vlc_mutex_unlock( &p_input->lock );
247     return err;
248 }
249
250 int input_ItemAddInfo( input_item_t *p_i,
251                             const char *psz_cat,
252                             const char *psz_name,
253                             const char *psz_format, ... )
254 {
255     va_list args;
256     int i;
257     info_t *p_info = NULL;
258     info_category_t *p_cat = NULL ;
259
260     vlc_mutex_lock( &p_i->lock );
261
262     for( i = 0 ; i < p_i->i_categories ; i ++ )
263     {
264         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
265         {
266             p_cat = p_i->pp_categories[i];
267             break;
268         }
269     }
270     if( !p_cat )
271     {
272         if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
273         {
274             vlc_mutex_unlock( &p_i->lock );
275             return VLC_ENOMEM;
276         }
277         p_cat->psz_name = strdup( psz_cat );
278         p_cat->i_infos = 0;
279         p_cat->pp_infos = 0;
280         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
281                      p_cat );
282     }
283
284     for( i = 0; i< p_cat->i_infos; i++ )
285     {
286         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
287         {
288             p_info = p_cat->pp_infos[i];
289             break;
290         }
291     }
292
293     if( !p_info )
294     {
295         if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
296         {
297             vlc_mutex_unlock( &p_i->lock );
298             return VLC_ENOMEM;
299         }
300         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
301         p_info->psz_name = strdup( psz_name );
302     }
303     else
304     {
305         free( p_info->psz_value );
306     }
307
308     va_start( args, psz_format );
309     if( vasprintf( &p_info->psz_value, psz_format, args) == -1 )
310         p_info->psz_value = NULL;
311     va_end( args );
312
313     vlc_mutex_unlock( &p_i->lock );
314
315     return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
316 }
317
318 input_item_t *__input_ItemGetById( vlc_object_t *p_obj, int i_id )
319 {
320     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
321     input_item_t * p_ret = NULL;
322     int i;
323
324     vlc_object_lock( p_obj->p_libvlc );
325
326     ARRAY_BSEARCH( priv->input_items, ->i_id, int, i_id, i);
327     if( i != -1 )
328         p_ret = ARRAY_VAL( priv->input_items, i);
329
330     vlc_object_unlock( p_obj->p_libvlc );
331
332     return p_ret;
333 }
334
335 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
336                                   const char *psz_name,
337                                   int i_options,
338                                   const char *const *ppsz_options,
339                                   mtime_t i_duration )
340 {
341     return input_ItemNewWithType( p_obj, psz_uri, psz_name,
342                                   i_options, ppsz_options,
343                                   i_duration, ITEM_TYPE_UNKNOWN );
344 }
345
346
347 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
348                                 const char *psz_name,
349                                 int i_options,
350                                 const char *const *ppsz_options,
351                                 mtime_t i_duration,
352                                 int i_type )
353 {
354     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
355
356     DECMALLOC_NULL( p_input, input_item_t );
357
358     input_ItemInit( p_obj, p_input );
359     vlc_gc_init( p_input, input_ItemDestroy, (void *)p_obj->p_libvlc );
360
361     vlc_object_lock( p_obj->p_libvlc );
362     p_input->i_id = ++priv->i_last_input_id;
363     ARRAY_APPEND( priv->input_items, p_input );
364     vlc_object_unlock( p_obj->p_libvlc );
365
366     p_input->b_fixed_name = false;
367
368     if( psz_uri )
369         p_input->psz_uri = strdup( psz_uri );
370     else
371         p_input->psz_uri = NULL;
372
373     p_input->i_type = i_type;
374
375     if( p_input->i_type == ITEM_TYPE_UNKNOWN )
376         GuessType( p_input );
377
378     if( psz_name != NULL )
379         p_input->psz_name = strdup( psz_name );
380     else if( p_input->i_type == ITEM_TYPE_FILE && p_input->psz_uri )
381     {
382         const char *psz_filename = strrchr( p_input->psz_uri, DIR_SEP_CHAR );
383         if( psz_filename && *psz_filename == DIR_SEP_CHAR )
384             psz_filename++;
385         p_input->psz_name = strdup( psz_filename && *psz_filename
386                                     ? psz_filename : p_input->psz_uri );
387     }
388     else
389         p_input->psz_name = p_input->psz_uri ? strdup( p_input->psz_uri ) : NULL;
390
391     p_input->i_duration = i_duration;
392
393     for( int i = 0; i < i_options; i++ )
394         input_ItemAddOption( p_input, ppsz_options[i] );
395     return p_input;
396 }
397
398 /* Guess the type of the item using the beginning of the mrl */
399 static void GuessType( input_item_t *p_item)
400 {
401     int i;
402     static struct { const char *psz_search; int i_type; }  types_array[] =
403     {
404         { "http", ITEM_TYPE_NET },
405         { "dvd", ITEM_TYPE_DISC },
406         { "cdda", ITEM_TYPE_CDDA },
407         { "mms", ITEM_TYPE_NET },
408         { "rtsp", ITEM_TYPE_NET },
409         { "udp", ITEM_TYPE_NET },
410         { "rtp", ITEM_TYPE_NET },
411         { "vcd", ITEM_TYPE_DISC },
412         { "v4l", ITEM_TYPE_CARD },
413         { "dshow", ITEM_TYPE_CARD },
414         { "pvr", ITEM_TYPE_CARD },
415         { "dvb", ITEM_TYPE_CARD },
416         { "qpsk", ITEM_TYPE_CARD },
417         { "sdp", ITEM_TYPE_NET },
418         { NULL, 0 }
419     };
420
421     if( !p_item->psz_uri )
422     {
423         p_item->i_type = ITEM_TYPE_FILE;
424         return;
425     }
426
427     for( i = 0; types_array[i].psz_search != NULL; i++ )
428     {
429         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
430                       strlen( types_array[i].psz_search ) ) )
431         {
432             p_item->i_type = types_array[i].i_type;
433             return;
434         }
435     }
436     p_item->i_type = ITEM_TYPE_FILE;
437 }