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