]> git.sesse.net Git - vlc/blob - src/input/item.c
Move/clean up input event code to its own file.
[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_common.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 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
38  * anyway. */
39 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
40 {
41     memset( p_i, 0, sizeof(input_item_t) );
42     p_i->psz_name = NULL;
43     p_i->psz_uri = NULL;
44     TAB_INIT( p_i->i_es, p_i->es );
45     TAB_INIT( p_i->i_options, p_i->ppsz_options );
46     p_i->optflagv = NULL, p_i->optflagc = 0;
47     TAB_INIT( p_i->i_categories, p_i->pp_categories );
48
49     p_i->i_type = ITEM_TYPE_UNKNOWN;
50     p_i->b_fixed_name = true;
51
52     p_i->p_stats = NULL;
53     p_i->p_meta = NULL;
54
55     vlc_mutex_init( &p_i->lock );
56     vlc_event_manager_t * p_em = &p_i->event_manager;
57     vlc_event_manager_init( p_em, p_i, p_o );
58     vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
59     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
60     vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
61     vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
62     vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
63     vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
64     vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
65 }
66
67 static inline void input_item_Clean( input_item_t *p_i )
68 {
69     int i;
70
71     vlc_event_manager_fini( &p_i->event_manager );
72
73     free( p_i->psz_name );
74     free( p_i->psz_uri );
75     if( p_i->p_stats )
76     {
77         vlc_mutex_destroy( &p_i->p_stats->lock );
78         free( p_i->p_stats );
79     }
80
81     if( p_i->p_meta )
82         vlc_meta_Delete( p_i->p_meta );
83
84     for( i = 0; i < p_i->i_options; i++ )
85         free( p_i->ppsz_options[i] );
86     TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
87     free( p_i->optflagv);
88
89     for( i = 0; i < p_i->i_es; i++ )
90     {
91         es_format_Clean( p_i->es[i] );
92         free( p_i->es[i] );
93     }
94     TAB_CLEAN( p_i->i_es, p_i->es );
95
96     for( i = 0; i < p_i->i_categories; i++ )
97     {
98         info_category_t *p_category = p_i->pp_categories[i];
99         int j;
100
101         for( j = 0; j < p_category->i_infos; j++ )
102         {
103             struct info_t *p_info = p_category->pp_infos[j];
104
105             free( p_info->psz_name);
106             free( p_info->psz_value );
107             free( p_info );
108         }
109         TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
110
111         free( p_category->psz_name );
112         free( p_category );
113     }
114     TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
115
116     vlc_mutex_destroy( &p_i->lock );
117 }
118
119 void input_item_SetHasErrorWhenReading( input_item_t *p_i, bool error )
120 {
121     vlc_event_t event;
122
123     if( p_i->b_error_when_reading == error )
124         return;
125
126     p_i->b_error_when_reading = error;
127
128     /* Notify interested third parties */
129     event.type = vlc_InputItemErrorWhenReadingChanged;
130     event.u.input_item_error_when_reading_changed.new_value = error;
131     vlc_event_send( &p_i->event_manager, &event );
132 }
133
134 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
135 {
136     vlc_event_t event;
137
138     vlc_mutex_lock( &p_i->lock );
139     if( !p_i->p_meta )
140         p_i->p_meta = vlc_meta_New();
141     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
142     vlc_mutex_unlock( &p_i->lock );
143
144     /* Notify interested third parties */
145     event.type = vlc_InputItemMetaChanged;
146     event.u.input_item_meta_changed.meta_type = meta_type;
147     vlc_event_send( &p_i->event_manager, &event );
148 }
149
150 /**
151  * Get the item from an input thread
152  */
153 input_item_t *input_GetItem( input_thread_t *p_input )
154 {
155     assert( p_input && p_input->p );
156     return p_input->p->input.p_item;
157 }
158
159 void input_item_CopyOptions( input_item_t *p_parent,
160                                           input_item_t *p_child )
161 {
162     int i;
163     for( i = 0 ; i< p_parent->i_options; i++ )
164     {
165         char *psz_option= strdup( p_parent->ppsz_options[i] );
166         if( !strcmp( psz_option, "meta-file" ) )
167         {
168             free( psz_option );
169             continue;
170         }
171         p_child->i_options++;
172         p_child->ppsz_options = (char **)realloc( p_child->ppsz_options,
173                                                   p_child->i_options *
174                                                   sizeof( char * ) );
175         p_child->ppsz_options[p_child->i_options-1] = psz_option;
176         p_child->optflagc++;
177         p_child->optflagv = (uint8_t *)realloc( p_child->optflagv,
178                                                 p_child->optflagc );
179         p_child->optflagv[p_child->optflagc - 1] = p_parent->optflagv[i];
180     }
181 }
182
183 void input_item_SetName( input_item_t *p_item, const char *psz_name )
184 {
185     free( p_item->psz_name );
186     p_item->psz_name = strdup( psz_name );
187 }
188
189 /* This won't hold the item, but can tell to interested third parties
190  * Like the playlist, that there is a new sub item. With this design
191  * It is not the input item's responsability to keep all the ref of
192  * the input item children. */
193 void input_item_AddSubItem( input_item_t *p_parent,
194                                          input_item_t *p_child )
195 {
196     vlc_event_t event;
197
198     p_parent->i_type = ITEM_TYPE_PLAYLIST;
199
200     /* Notify interested third parties */
201     event.type = vlc_InputItemSubItemAdded;
202     event.u.input_item_subitem_added.p_new_child = p_child;
203     vlc_event_send( &p_parent->event_manager, &event );
204 }
205
206 int input_item_AddOption (input_item_t *item, const char *str)
207 {
208     return input_item_AddOpt (item, str, VLC_INPUT_OPTION_TRUSTED);
209 }
210
211 bool input_item_HasErrorWhenReading (input_item_t *item)
212 {
213     return item->b_error_when_reading;
214 }
215
216 bool input_item_MetaMatch( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz )
217 {
218     vlc_mutex_lock( &p_i->lock );
219     if( !p_i->p_meta )
220     {
221         vlc_mutex_unlock( &p_i->lock );
222         return false;
223     }
224     const char * meta = vlc_meta_Get( p_i->p_meta, meta_type );
225     bool ret = meta && strcasestr( meta, psz );
226     vlc_mutex_unlock( &p_i->lock );
227
228     return ret;
229 }
230
231 char * input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
232 {
233     char * psz = NULL;
234     vlc_mutex_lock( &p_i->lock );
235
236     if( !p_i->p_meta )
237     {
238         vlc_mutex_unlock( &p_i->lock );
239         return NULL;
240     }
241
242     if( vlc_meta_Get( p_i->p_meta, meta_type ) )
243         psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
244
245     vlc_mutex_unlock( &p_i->lock );
246     return psz;
247 }
248
249 char * input_item_GetName( input_item_t * p_i )
250 {
251     vlc_mutex_lock( &p_i->lock );
252     char *psz_s = p_i->psz_name ? strdup( p_i->psz_name ) : NULL;
253     vlc_mutex_unlock( &p_i->lock );
254     return psz_s;
255 }
256
257 char * input_item_GetURI( input_item_t * p_i )
258 {
259     vlc_mutex_lock( &p_i->lock );
260     char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
261     vlc_mutex_unlock( &p_i->lock );
262     return psz_s;
263 }
264
265 void input_item_SetURI( input_item_t * p_i, char * psz_uri )
266 {
267     vlc_mutex_lock( &p_i->lock );
268     free( p_i->psz_uri );
269     p_i->psz_uri = strdup( psz_uri );
270     vlc_mutex_unlock( &p_i->lock );
271 }
272
273 mtime_t input_item_GetDuration( input_item_t * p_i )
274 {
275     vlc_mutex_lock( &p_i->lock );
276     mtime_t i_duration = p_i->i_duration;
277     vlc_mutex_unlock( &p_i->lock );
278     return i_duration;
279 }
280
281 void input_item_SetDuration( input_item_t * p_i, mtime_t i_duration )
282 {
283     bool send_event = false;
284
285     vlc_mutex_lock( &p_i->lock );
286     if( p_i->i_duration != i_duration )
287     {
288         p_i->i_duration = i_duration;
289         send_event = true;
290     }
291     vlc_mutex_unlock( &p_i->lock );
292
293     if ( send_event == true )
294     {
295         vlc_event_t event;
296         event.type = vlc_InputItemDurationChanged;
297         event.u.input_item_duration_changed.new_duration = i_duration;
298         vlc_event_send( &p_i->event_manager, &event );
299     }
300
301     return;
302 }
303
304
305 bool input_item_IsPreparsed( input_item_t *p_i )
306 {
307     return p_i->p_meta ? p_i->p_meta->i_status & ITEM_PREPARSED : false ;
308 }
309
310 bool input_item_IsArtFetched( input_item_t *p_i )
311 {
312     return p_i->p_meta ? p_i->p_meta->i_status & ITEM_ART_FETCHED : false ;
313 }
314
315 /* FIXME dangerous, unlocked */
316 const vlc_meta_t * input_item_GetMetaObject( input_item_t *p_i )
317 {
318     if( !p_i->p_meta )
319         p_i->p_meta = vlc_meta_New();
320
321     return p_i->p_meta;
322 }
323
324 /* FIXME dangerous, unlocked */
325 void input_item_MetaMerge( input_item_t *p_i, const vlc_meta_t * p_new_meta )
326 {
327     if( !p_i->p_meta )
328         p_i->p_meta = vlc_meta_New();
329
330     vlc_meta_Merge( p_i->p_meta, p_new_meta );
331 }
332
333 /**
334  * Get a info item from a given category in a given input item.
335  *
336  * \param p_i The input item to get info from
337  * \param psz_cat String representing the category for the info
338  * \param psz_name String representing the name of the desired info
339  * \return A pointer to the string with the given info if found, or an
340  *         empty string otherwise. The caller should free the returned
341  *         pointer.
342  */
343 char *input_item_GetInfo( input_item_t *p_i,
344                               const char *psz_cat,
345                               const char *psz_name )
346 {
347     int i,j;
348
349     vlc_mutex_lock( &p_i->lock );
350
351     for( i = 0 ; i< p_i->i_categories  ; i++ )
352     {
353         info_category_t *p_cat = p_i->pp_categories[i];
354
355         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
356             continue;
357
358         for( j = 0; j < p_cat->i_infos ; j++ )
359         {
360             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
361             {
362                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
363                 vlc_mutex_unlock( &p_i->lock );
364                 return psz_ret;
365             }
366         }
367     }
368     vlc_mutex_unlock( &p_i->lock );
369     return strdup( "" );
370 }
371
372 static void input_item_Destroy ( gc_object_t *gc )
373 {
374     input_item_t *p_input = vlc_priv(gc, input_item_t);
375
376     input_item_Clean( p_input );
377     free( p_input );
378 }
379
380 int input_item_AddOpt( input_item_t *p_input, const char *psz_option,
381                       unsigned flags )
382 {
383     int err = VLC_SUCCESS;
384
385     if( psz_option == NULL )
386         return VLC_EGENERIC;
387
388     vlc_mutex_lock( &p_input->lock );
389     if (flags & VLC_INPUT_OPTION_UNIQUE)
390     {
391         for (int i = 0 ; i < p_input->i_options; i++)
392             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
393                 goto out;
394     }
395
396     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
397     if (flagv == NULL)
398     {
399         err = VLC_ENOMEM;
400         goto out;
401     }
402     p_input->optflagv = flagv;
403     flagv[p_input->optflagc++] = flags;
404
405     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
406                  p_input->i_options, strdup( psz_option ) );
407 out:
408     vlc_mutex_unlock( &p_input->lock );
409     return err;
410 }
411
412 int input_item_AddInfo( input_item_t *p_i,
413                             const char *psz_cat,
414                             const char *psz_name,
415                             const char *psz_format, ... )
416 {
417     va_list args;
418     int i;
419     info_t *p_info = NULL;
420     info_category_t *p_cat = NULL ;
421
422     vlc_mutex_lock( &p_i->lock );
423
424     for( i = 0 ; i < p_i->i_categories ; i ++ )
425     {
426         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
427         {
428             p_cat = p_i->pp_categories[i];
429             break;
430         }
431     }
432     if( !p_cat )
433     {
434         if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
435         {
436             vlc_mutex_unlock( &p_i->lock );
437             return VLC_ENOMEM;
438         }
439         p_cat->psz_name = strdup( psz_cat );
440         p_cat->i_infos = 0;
441         p_cat->pp_infos = 0;
442         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
443                      p_cat );
444     }
445
446     for( i = 0; i< p_cat->i_infos; i++ )
447     {
448         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
449         {
450             p_info = p_cat->pp_infos[i];
451             break;
452         }
453     }
454
455     if( !p_info )
456     {
457         if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
458         {
459             vlc_mutex_unlock( &p_i->lock );
460             return VLC_ENOMEM;
461         }
462         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
463         p_info->psz_name = strdup( psz_name );
464     }
465     else
466     {
467         free( p_info->psz_value );
468     }
469
470     va_start( args, psz_format );
471     if( vasprintf( &p_info->psz_value, psz_format, args) == -1 )
472         p_info->psz_value = NULL;
473     va_end( args );
474
475     vlc_mutex_unlock( &p_i->lock );
476
477     if( p_info->psz_value )
478     {
479         vlc_event_t event;
480
481         event.type = vlc_InputItemInfoChanged;
482         vlc_event_send( &p_i->event_manager, &event );
483     }
484     return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
485 }
486 int input_item_DelInfo( input_item_t *p_i,
487                         const char *psz_cat,
488                         const char *psz_name )
489 {
490     info_category_t *p_cat = NULL;
491     int i_cat;
492     int i;
493
494     vlc_mutex_lock( &p_i->lock );
495     for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
496     {
497         if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
498                      psz_cat ) )
499         {
500             p_cat = p_i->pp_categories[i_cat];
501             break;
502         }
503     }
504     if( p_cat == NULL )
505     {
506         vlc_mutex_unlock( &p_i->lock );
507         return VLC_EGENERIC;
508     }
509
510     if( psz_name )
511     {
512         /* Remove a specific info */
513         for( i = 0; i < p_cat->i_infos; i++ )
514         {
515             if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
516             {
517                 free( p_cat->pp_infos[i]->psz_name );
518                 if( p_cat->pp_infos[i]->psz_value )
519                     free( p_cat->pp_infos[i]->psz_value );
520                 free( p_cat->pp_infos[i] );
521                 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
522                 break;
523             }
524         }
525         if( i >= p_cat->i_infos )
526         {
527             vlc_mutex_unlock( &p_i->lock );
528             return VLC_EGENERIC;
529         }
530     }
531     else
532     {
533         /* Remove the complete categorie */
534         for( i = 0; i < p_cat->i_infos; i++ )
535         {
536             free( p_cat->pp_infos[i]->psz_name );
537             if( p_cat->pp_infos[i]->psz_value )
538                 free( p_cat->pp_infos[i]->psz_value );
539             free( p_cat->pp_infos[i] );
540         }
541         if( p_cat->pp_infos )
542             free( p_cat->pp_infos );
543         REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
544     }
545     vlc_mutex_unlock( &p_i->lock );
546
547
548     vlc_event_t event;
549     event.type = vlc_InputItemInfoChanged;
550     vlc_event_send( &p_i->event_manager, &event );
551
552     return VLC_SUCCESS;
553 }
554
555
556 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
557                                   const char *psz_name,
558                                   int i_options,
559                                   const char *const *ppsz_options,
560                                   mtime_t i_duration )
561 {
562     return input_item_NewWithType( p_obj, psz_uri, psz_name,
563                                   i_options, ppsz_options,
564                                   i_duration, ITEM_TYPE_UNKNOWN );
565 }
566
567
568 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
569                                 const char *psz_name,
570                                 int i_options,
571                                 const char *const *ppsz_options,
572                                 mtime_t i_duration,
573                                 int i_type )
574 {
575     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
576
577     input_item_t* p_input = malloc( sizeof(input_item_t ) );
578     if( !p_input )
579         return NULL;
580
581     input_item_Init( p_obj, p_input );
582     vlc_gc_init( p_input, input_item_Destroy );
583
584     vlc_object_lock( p_obj->p_libvlc );
585     p_input->i_id = ++priv->i_last_input_id;
586     vlc_object_unlock( p_obj->p_libvlc );
587
588     p_input->b_fixed_name = false;
589
590     if( psz_uri )
591         p_input->psz_uri = strdup( psz_uri );
592     else
593         p_input->psz_uri = NULL;
594
595     p_input->i_type = i_type;
596     p_input->b_prefers_tree = false;
597
598     if( p_input->i_type == ITEM_TYPE_UNKNOWN )
599         GuessType( p_input );
600
601     if( psz_name != NULL )
602         p_input->psz_name = strdup( psz_name );
603     else if( p_input->i_type == ITEM_TYPE_FILE && p_input->psz_uri )
604     {
605         const char *psz_filename = strrchr( p_input->psz_uri, DIR_SEP_CHAR );
606         if( psz_filename && *psz_filename == DIR_SEP_CHAR )
607             psz_filename++;
608         p_input->psz_name = strdup( psz_filename && *psz_filename
609                                     ? psz_filename : p_input->psz_uri );
610     }
611     else
612         p_input->psz_name = p_input->psz_uri ? strdup( p_input->psz_uri ) : NULL;
613
614     p_input->i_duration = i_duration;
615
616     for( int i = 0; i < i_options; i++ )
617         input_item_AddOption( p_input, ppsz_options[i] );
618     return p_input;
619 }
620
621 /* Guess the type of the item using the beginning of the mrl */
622 static void GuessType( input_item_t *p_item)
623 {
624     int i;
625     static struct { const char *psz_search; int i_type; }  types_array[] =
626     {
627         { "http", ITEM_TYPE_NET },
628         { "dvd", ITEM_TYPE_DISC },
629         { "cdda", ITEM_TYPE_CDDA },
630         { "mms", ITEM_TYPE_NET },
631         { "rtsp", ITEM_TYPE_NET },
632         { "udp", ITEM_TYPE_NET },
633         { "rtp", ITEM_TYPE_NET },
634         { "vcd", ITEM_TYPE_DISC },
635         { "v4l", ITEM_TYPE_CARD },
636         { "dshow", ITEM_TYPE_CARD },
637         { "pvr", ITEM_TYPE_CARD },
638         { "dvb", ITEM_TYPE_CARD },
639         { "qpsk", ITEM_TYPE_CARD },
640         { "sdp", ITEM_TYPE_NET },
641         { NULL, 0 }
642     };
643
644     if( !p_item->psz_uri )
645     {
646         p_item->i_type = ITEM_TYPE_FILE;
647         return;
648     }
649
650     for( i = 0; types_array[i].psz_search != NULL; i++ )
651     {
652         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
653                       strlen( types_array[i].psz_search ) ) )
654         {
655             p_item->i_type = types_array[i].i_type;
656             return;
657         }
658     }
659     p_item->i_type = ITEM_TYPE_FILE;
660 }