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