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