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