]> git.sesse.net Git - vlc/blob - src/input/item.c
input: Allow NULL item name.
[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/vlc.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 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
38 {
39     vlc_event_t event;
40
41     vlc_mutex_lock( &p_i->lock );
42     if( !p_i->p_meta )
43         p_i->p_meta = vlc_meta_New();
44     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
45     vlc_mutex_unlock( &p_i->lock );
46
47     /* Notify interested third parties */
48     event.type = vlc_InputItemMetaChanged;
49     event.u.input_item_meta_changed.meta_type = meta_type;
50     vlc_event_send( &p_i->event_manager, &event );
51 }
52
53 /**
54  * Get the item from an input thread
55  */
56 input_item_t *input_GetItem( input_thread_t *p_input )
57 {
58     assert( p_input && p_input->p );
59     return p_input->p->input.p_item;
60 }
61
62 /**
63  * Get a info item from a given category in a given input item.
64  *
65  * \param p_i The input item to get info from
66  * \param psz_cat String representing the category for the info
67  * \param psz_name String representing the name of the desired info
68  * \return A pointer to the string with the given info if found, or an
69  *         empty string otherwise. The caller should free the returned
70  *         pointer.
71  */
72 char *input_ItemGetInfo( input_item_t *p_i,
73                               const char *psz_cat,
74                               const char *psz_name )
75 {
76     int i,j;
77
78     vlc_mutex_lock( &p_i->lock );
79
80     for( i = 0 ; i< p_i->i_categories  ; i++ )
81     {
82         info_category_t *p_cat = p_i->pp_categories[i];
83
84         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
85             continue;
86
87         for( j = 0; j < p_cat->i_infos ; j++ )
88         {
89             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
90             {
91                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
92                 vlc_mutex_unlock( &p_i->lock );
93                 return psz_ret;
94             }
95         }
96     }
97     vlc_mutex_unlock( &p_i->lock );
98     return strdup( "" );
99 }
100
101 static void input_ItemDestroy ( gc_object_t *p_this )
102 {
103     vlc_object_t *p_obj = (vlc_object_t *)p_this->p_destructor_arg;
104     input_item_t *p_input = (input_item_t *) p_this;
105     int i;
106
107     input_ItemClean( p_input );
108
109     vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
110
111     ARRAY_BSEARCH( p_obj->p_libvlc->input_items,->i_id, int, p_input->i_id, i);
112     if( i != -1 )
113         ARRAY_REMOVE( p_obj->p_libvlc->input_items, i);
114
115     vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
116
117     free( p_input );
118 }
119
120 int input_ItemAddOpt( input_item_t *p_input, const char *psz_option,
121                       unsigned flags )
122 {
123     int err = VLC_SUCCESS;
124
125     if( psz_option == NULL )
126         return VLC_EGENERIC;
127
128     vlc_mutex_lock( &p_input->lock );
129     if (flags & VLC_INPUT_OPTION_UNIQUE)
130     {
131         for (int i = 0 ; i < p_input->i_options; i++)
132             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
133                 goto out;
134     }
135
136     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
137     if (flagv == NULL)
138     {
139         err = VLC_ENOMEM;
140         goto out;
141     }
142     p_input->optflagv = flagv;
143     flagv[p_input->optflagc++] = flags;
144
145     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
146                  p_input->i_options, strdup( psz_option ) );
147 out:
148     vlc_mutex_unlock( &p_input->lock );
149     return err;
150 }
151
152 int input_ItemAddInfo( input_item_t *p_i,
153                             const char *psz_cat,
154                             const char *psz_name,
155                             const char *psz_format, ... )
156 {
157     va_list args;
158     int i;
159     info_t *p_info = NULL;
160     info_category_t *p_cat = NULL ;
161
162     vlc_mutex_lock( &p_i->lock );
163
164     for( i = 0 ; i < p_i->i_categories ; i ++ )
165     {
166         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
167         {
168             p_cat = p_i->pp_categories[i];
169             break;
170         }
171     }
172     if( !p_cat )
173     {
174         if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
175         {
176             vlc_mutex_unlock( &p_i->lock );
177             return VLC_ENOMEM;
178         }
179         p_cat->psz_name = strdup( psz_cat );
180         p_cat->i_infos = 0;
181         p_cat->pp_infos = 0;
182         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
183                      p_cat );
184     }
185
186     for( i = 0; i< p_cat->i_infos; i++ )
187     {
188         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
189         {
190             p_info = p_cat->pp_infos[i];
191             break;
192         }
193     }
194
195     if( !p_info )
196     {
197         if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
198         {
199             vlc_mutex_unlock( &p_i->lock );
200             return VLC_ENOMEM;
201         }
202         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
203         p_info->psz_name = strdup( psz_name );
204     }
205     else
206     {
207         free( p_info->psz_value );
208     }
209
210     va_start( args, psz_format );
211     if( vasprintf( &p_info->psz_value, psz_format, args) )
212         p_info->psz_value = NULL;
213     va_end( args );
214
215     vlc_mutex_unlock( &p_i->lock );
216
217     return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
218 }
219
220 input_item_t *__input_ItemGetById( vlc_object_t *p_obj, int i_id )
221 {
222     input_item_t * p_ret = NULL;
223     int i;
224
225     vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
226
227     ARRAY_BSEARCH( p_obj->p_libvlc->input_items, ->i_id, int, i_id, i);
228     if( i != -1 )
229         p_ret = ARRAY_VAL( p_obj->p_libvlc->input_items, i);
230
231     vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
232
233     return p_ret;
234 }
235
236 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
237                                   const char *psz_name,
238                                   int i_options,
239                                   const char *const *ppsz_options,
240                                   mtime_t i_duration )
241 {
242     return input_ItemNewWithType( p_obj, psz_uri, psz_name,
243                                   i_options, ppsz_options,
244                                   i_duration, ITEM_TYPE_UNKNOWN );
245 }
246
247
248 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
249                                 const char *psz_name,
250                                 int i_options,
251                                 const char *const *ppsz_options,
252                                 mtime_t i_duration,
253                                 int i_type )
254 {
255     DECMALLOC_NULL( p_input, input_item_t );
256
257     input_ItemInit( p_obj, p_input );
258     vlc_gc_init( p_input, input_ItemDestroy, (void *)p_obj );
259
260     vlc_mutex_lock( &p_obj->p_libvlc->object_lock );
261     p_input->i_id = ++p_obj->p_libvlc->i_last_input_id;
262     ARRAY_APPEND( p_obj->p_libvlc->input_items, p_input );
263     vlc_mutex_unlock( &p_obj->p_libvlc->object_lock );
264
265     p_input->b_fixed_name = VLC_FALSE;
266
267     if( psz_uri )
268         p_input->psz_uri = strdup( psz_uri );
269     else
270         p_input->psz_uri = NULL;
271
272     p_input->i_type = i_type;
273     p_input->b_prefers_tree = VLC_FALSE;
274
275     if( p_input->i_type == ITEM_TYPE_UNKNOWN )
276         GuessType( p_input );
277
278     if( psz_name != NULL )
279         p_input->psz_name = strdup( psz_name );
280     else if( p_input->i_type == ITEM_TYPE_FILE && p_input->psz_uri )
281     {
282         const char *psz_filename = strrchr( p_input->psz_uri, DIR_SEP_CHAR );
283         if( psz_filename && *psz_filename == DIR_SEP_CHAR )
284             psz_filename++;
285         p_input->psz_name = strdup( psz_filename && *psz_filename
286                                     ? psz_filename : p_input->psz_uri );
287     }
288     else
289         p_input->psz_name = p_input->psz_uri ? strdup( p_input->psz_uri ) : NULL;
290
291     p_input->i_duration = i_duration;
292
293     for( int i = 0; i < i_options; i++ )
294         input_ItemAddOption( p_input, ppsz_options[i] );
295     return p_input;
296 }
297
298 /* Guess the type of the item using the beginning of the mrl */
299 static void GuessType( input_item_t *p_item)
300 {
301     int i;
302     static struct { const char *psz_search; int i_type; }  types_array[] =
303     {
304         { "http", ITEM_TYPE_NET },
305         { "dvd", ITEM_TYPE_DISC },
306         { "cdda", ITEM_TYPE_CDDA },
307         { "mms", ITEM_TYPE_NET },
308         { "rtsp", ITEM_TYPE_NET },
309         { "udp", ITEM_TYPE_NET },
310         { "rtp", ITEM_TYPE_NET },
311         { "vcd", ITEM_TYPE_DISC },
312         { "v4l", ITEM_TYPE_CARD },
313         { "dshow", ITEM_TYPE_CARD },
314         { "pvr", ITEM_TYPE_CARD },
315         { "dvb", ITEM_TYPE_CARD },
316         { "qpsk", ITEM_TYPE_CARD },
317         { "sdp", ITEM_TYPE_NET },
318         { NULL, 0 }
319     };
320
321     if( !p_item->psz_uri )
322     {
323         p_item->i_type = ITEM_TYPE_FILE;
324         return;
325     }
326
327     for( i = 0; types_array[i].psz_search != NULL; i++ )
328     {
329         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
330                       strlen( types_array[i].psz_search ) ) )
331         {
332             p_item->i_type = types_array[i].i_type;
333             return;
334         }
335     }
336     p_item->i_type = ITEM_TYPE_FILE;
337 }