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