]> git.sesse.net Git - vlc/blob - src/input/item.c
Rename vlc_input_item_* functions to input_Item* for consistency
[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     for( i = 0 ; i< p_playlist->i_input_items ; i++ )
80     {
81         if( p_playlist->pp_input_items[i]->i_id == p_input->i_id )
82         {
83             REMOVE_ELEM( p_playlist->pp_input_items,
84                          p_playlist->i_input_items, i );
85             break;
86         }
87     }
88     pl_Release( p_obj );
89     free( p_input );
90 }
91
92 void input_ItemAddOption( input_item_t *p_input,
93                           const char *psz_option )
94 {
95     if( !psz_option ) return;
96     vlc_mutex_lock( &p_input->lock );
97     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
98                  p_input->i_options, strdup( psz_option ) );
99     vlc_mutex_unlock( &p_input->lock );
100 }
101
102 void input_ItemAddOptionNoDup( input_item_t *p_input,
103                                const char *psz_option )
104 {
105     int i;
106     if( !psz_option ) return ;
107     vlc_mutex_lock( &p_input->lock );
108     for( i = 0 ; i< p_input->i_options; i++ )
109     {
110         if( !strcmp( p_input->ppsz_options[i], psz_option ) )
111         {
112             vlc_mutex_unlock(& p_input->lock );
113             return;
114         }
115     }
116     TAB_APPEND( p_input->i_options, p_input->ppsz_options, strdup( psz_option));    vlc_mutex_unlock( &p_input->lock );
117 }
118
119
120 int input_ItemAddInfo( input_item_t *p_i,
121                             const char *psz_cat,
122                             const char *psz_name,
123                             const char *psz_format, ... )
124 {
125     va_list args;
126     int i;
127     info_t *p_info = NULL;
128     info_category_t *p_cat = NULL ;
129
130     vlc_mutex_lock( &p_i->lock );
131
132     for( i = 0 ; i < p_i->i_categories ; i ++ )
133     {
134         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
135         {
136             p_cat = p_i->pp_categories[i];
137             break;
138         }
139     }
140     if( !p_cat )
141     {
142         if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
143         {
144             vlc_mutex_unlock( &p_i->lock );
145             return VLC_EGENERIC;
146         }
147         p_cat->psz_name = strdup( psz_cat );
148         p_cat->i_infos = 0;
149         p_cat->pp_infos = 0;
150         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
151                      p_cat );
152     }
153
154     for( i = 0; i< p_cat->i_infos; i++ )
155     {
156         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
157         {
158             p_info = p_cat->pp_infos[i];
159             break;
160         }
161     }
162
163     if( !p_info )
164     {
165         if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
166         {
167             vlc_mutex_unlock( &p_i->lock );
168             return VLC_EGENERIC;
169         }
170         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
171         p_info->psz_name = strdup( psz_name );
172     }
173     else
174     {
175         if( p_info->psz_value ) free( p_info->psz_value );
176     }
177
178     va_start( args, psz_format );
179     vasprintf( &p_info->psz_value, psz_format, args);
180     va_end( args );
181
182     vlc_mutex_unlock( &p_i->lock );
183
184     return VLC_SUCCESS;
185 }
186
187 input_item_t *input_ItemGetById( playlist_t *p_playlist, int i_id )
188 {
189     int i, i_top, i_bottom;
190     i_bottom = 0; i_top = p_playlist->i_input_items -1;
191     i = i_top  /2 ;
192     while( p_playlist->pp_input_items[i]->i_id != i_id &&
193            i_top > i_bottom )
194     {
195         if( p_playlist->pp_input_items[i]->i_id < i_id )
196             i_bottom = i + 1;
197         else
198             i_top = i - 1;
199         i = i_bottom + ( i_top - i_bottom ) / 2;
200     }
201     if( p_playlist->pp_input_items[i]->i_id == i_id )
202     {
203         return p_playlist->pp_input_items[i];
204     }
205     return NULL;
206 }
207
208 input_item_t *__input_ItemNewExt( vlc_object_t *p_obj, const char *psz_uri,
209                                   const char *psz_name, int i_options,
210                                   const char **ppsz_options, int i_duration )
211 {
212     return input_ItemNewWithType( p_obj, psz_uri, psz_name,
213                                   i_options, ppsz_options,
214                                   i_duration, ITEM_TYPE_UNKNOWN );
215 }
216
217
218 input_item_t *input_ItemNewWithType( vlc_object_t *p_obj, const char *psz_uri,
219                                 const char *psz_name, int i_options,
220                                 const char **ppsz_options, int i_duration,
221                                 int i_type )
222 {
223     playlist_t *p_playlist = pl_Yield( p_obj );
224     DECMALLOC_NULL( p_input, input_item_t );
225
226     input_ItemInit( p_obj, p_input );
227     vlc_gc_init( p_input, input_ItemDestroy, (void *)p_obj );
228
229     PL_LOCK;
230     p_input->i_id = ++p_playlist->i_last_input_id;
231     TAB_APPEND( p_playlist->i_input_items,
232                 p_playlist->pp_input_items,
233                 p_input );
234     PL_UNLOCK;
235     pl_Release( p_obj );
236
237     p_input->b_fixed_name = VLC_FALSE;
238
239     if( psz_uri )
240         p_input->psz_uri = strdup( psz_uri );
241     else
242         p_input->psz_uri = NULL;
243
244     if( psz_name != NULL )
245         p_input->psz_name = strdup( psz_name );
246     else
247         p_input->psz_name = strdup ( p_input->psz_uri );
248
249     p_input->i_type = i_type;
250     p_input->b_prefers_tree = VLC_FALSE;
251
252     if( p_input->i_type == ITEM_TYPE_UNKNOWN )
253         GuessType( p_input );
254
255     p_input->i_duration = i_duration;
256     p_input->ppsz_options = NULL;
257
258     for( p_input->i_options = 0; p_input->i_options < i_options;
259          p_input->i_options++ )
260     {
261         if( !p_input->i_options )
262         {
263             p_input->ppsz_options = malloc( i_options * sizeof(char *) );
264             if( !p_input->ppsz_options ) break;
265         }
266         p_input->ppsz_options[p_input->i_options] =
267                     strdup( ppsz_options[p_input->i_options] );
268     }
269     return p_input;
270 }
271
272 /* Guess the type of the item using the beginning of the mrl */
273 static void GuessType( input_item_t *p_item)
274 {
275     int i;
276     static struct { char *psz_search; int i_type; }  types_array[] =
277     {
278         { "http", ITEM_TYPE_NET },
279         { "dvd", ITEM_TYPE_DISC },
280         { "cdda", ITEM_TYPE_CDDA },
281         { "mms", ITEM_TYPE_NET },
282         { "rtsp", ITEM_TYPE_NET },
283         { "udp", ITEM_TYPE_NET },
284         { "rtp", ITEM_TYPE_NET },
285         { "vcd", ITEM_TYPE_DISC },
286         { "v4l", ITEM_TYPE_CARD },
287         { "dshow", ITEM_TYPE_CARD },
288         { "pvr", ITEM_TYPE_CARD },
289         { "dvb", ITEM_TYPE_CARD },
290         { "qpsk", ITEM_TYPE_CARD },
291         { "sdp", ITEM_TYPE_NET },
292         { NULL, 0 }
293     };
294
295 #if 0 /* Unused */
296     static struct { char *psz_search; int i_type; } exts_array[] =
297     {
298         { "mp3", ITEM_TYPE_AFILE },
299         { NULL, 0 }
300     };
301 #endif
302
303     for( i = 0; types_array[i].psz_search != NULL; i++ )
304     {
305         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
306                       strlen( types_array[i].psz_search ) ) )
307         {
308             p_item->i_type = types_array[i].i_type;
309             return;
310         }
311     }
312     p_item->i_type = ITEM_TYPE_VFILE;
313 }