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