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