1 /*****************************************************************************
2 * item.c: input_item management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include "vlc_playlist.h"
31 #include "vlc_interface.h"
35 static void GuessType( input_item_t *p_item );
37 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
39 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
41 memset( p_i, 0, sizeof(input_item_t) );
45 TAB_INIT( p_i->i_es, p_i->es );
46 TAB_INIT( p_i->i_options, p_i->ppsz_options );
47 p_i->optflagv = NULL, p_i->optflagc = 0;
48 TAB_INIT( p_i->i_categories, p_i->pp_categories );
50 p_i->i_type = ITEM_TYPE_UNKNOWN;
51 p_i->b_fixed_name = true;
56 vlc_mutex_init( &p_i->lock );
57 vlc_event_manager_t * p_em = &p_i->event_manager;
58 vlc_event_manager_init( p_em, p_i, p_o );
59 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
60 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
61 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
62 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
63 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
64 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
65 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
68 static inline void input_item_Clean( input_item_t *p_i )
72 vlc_event_manager_fini( &p_i->event_manager );
74 free( p_i->psz_name );
78 vlc_mutex_destroy( &p_i->p_stats->lock );
83 vlc_meta_Delete( p_i->p_meta );
85 for( i = 0; i < p_i->i_options; i++ )
86 free( p_i->ppsz_options[i] );
87 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
90 for( i = 0; i < p_i->i_es; i++ )
92 es_format_Clean( p_i->es[i] );
95 TAB_CLEAN( p_i->i_es, p_i->es );
97 for( i = 0; i < p_i->i_categories; i++ )
99 info_category_t *p_category = p_i->pp_categories[i];
102 for( j = 0; j < p_category->i_infos; j++ )
104 struct info_t *p_info = p_category->pp_infos[j];
106 free( p_info->psz_name);
107 free( p_info->psz_value );
110 TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
112 free( p_category->psz_name );
115 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
117 vlc_mutex_destroy( &p_i->lock );
119 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
123 vlc_mutex_lock( &p_i->lock );
125 b_changed = p_i->b_error_when_reading != b_error;
126 p_i->b_error_when_reading = b_error;
128 vlc_mutex_unlock( &p_i->lock );
134 event.type = vlc_InputItemErrorWhenReadingChanged;
135 event.u.input_item_error_when_reading_changed.new_value = b_error;
136 vlc_event_send( &p_i->event_manager, &event );
139 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
141 bool b_send_event = false;
143 vlc_mutex_lock( &p_i->lock );
146 p_i->p_meta = vlc_meta_New();
150 i_new_status = p_i->p_meta->i_status | ITEM_PREPARSED;
152 i_new_status = p_i->p_meta->i_status & ~ITEM_PREPARSED;
153 if( p_i->p_meta->i_status != i_new_status )
155 p_i->p_meta->i_status = i_new_status;
159 vlc_mutex_unlock( &p_i->lock );
164 event.type = vlc_InputItemPreparsedChanged;
165 event.u.input_item_preparsed_changed.new_status = i_new_status;
166 vlc_event_send( &p_i->event_manager, &event );
169 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
171 vlc_mutex_lock( &p_i->lock );
174 p_i->p_meta = vlc_meta_New();
177 p_i->p_meta->i_status |= ITEM_ART_NOTFOUND;
179 p_i->p_meta->i_status &= ~ITEM_ART_NOTFOUND;
181 vlc_mutex_unlock( &p_i->lock );
183 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
185 vlc_mutex_lock( &p_i->lock );
188 p_i->p_meta = vlc_meta_New();
191 p_i->p_meta->i_status |= ITEM_ART_FETCHED;
193 p_i->p_meta->i_status &= ~ITEM_ART_FETCHED;
195 vlc_mutex_unlock( &p_i->lock );
198 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
202 vlc_mutex_lock( &p_i->lock );
204 p_i->p_meta = vlc_meta_New();
205 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
206 vlc_mutex_unlock( &p_i->lock );
208 /* Notify interested third parties */
209 event.type = vlc_InputItemMetaChanged;
210 event.u.input_item_meta_changed.meta_type = meta_type;
211 vlc_event_send( &p_i->event_manager, &event );
214 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
215 * consistant with (nearly?) all or copy funcs */
216 void input_item_CopyOptions( input_item_t *p_parent,
217 input_item_t *p_child )
219 vlc_mutex_lock( &p_parent->lock );
221 for( int i = 0 ; i< p_parent->i_options; i++ )
223 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
226 input_item_AddOption( p_child,
227 p_parent->ppsz_options[i],
228 p_parent->optflagv[i] );
231 vlc_mutex_unlock( &p_parent->lock );
234 /* This won't hold the item, but can tell to interested third parties
235 * Like the playlist, that there is a new sub item. With this design
236 * It is not the input item's responsability to keep all the ref of
237 * the input item children. */
238 void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
240 vlc_mutex_lock( &p_parent->lock );
242 p_parent->i_type = ITEM_TYPE_PLAYLIST;
244 vlc_mutex_unlock( &p_parent->lock );
246 /* Notify interested third parties */
249 event.type = vlc_InputItemSubItemAdded;
250 event.u.input_item_subitem_added.p_new_child = p_child;
251 vlc_event_send( &p_parent->event_manager, &event );
254 bool input_item_HasErrorWhenReading( input_item_t *p_item )
256 vlc_mutex_lock( &p_item->lock );
258 bool b_error = p_item->b_error_when_reading;
260 vlc_mutex_unlock( &p_item->lock );
265 bool input_item_MetaMatch( input_item_t *p_i,
266 vlc_meta_type_t meta_type, const char *psz )
268 vlc_mutex_lock( &p_i->lock );
272 vlc_mutex_unlock( &p_i->lock );
275 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
276 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
278 vlc_mutex_unlock( &p_i->lock );
283 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
285 vlc_mutex_lock( &p_i->lock );
289 vlc_mutex_unlock( &p_i->lock );
294 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
295 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
297 vlc_mutex_unlock( &p_i->lock );
301 /* Get the title of a given item or fallback to the name if the title is empty */
302 char *input_item_GetTitleFbName( input_item_t *p_item )
305 vlc_mutex_lock( &p_item->lock );
307 if( !p_item->p_meta )
309 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
310 vlc_mutex_unlock( &p_item->lock );
314 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
315 if( !EMPTY_STR( psz_title ) )
316 psz_ret = strdup( psz_title );
318 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
320 vlc_mutex_unlock( &p_item->lock );
324 char *input_item_GetName( input_item_t *p_item )
326 vlc_mutex_lock( &p_item->lock );
328 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
330 vlc_mutex_unlock( &p_item->lock );
333 void input_item_SetName( input_item_t *p_item, const char *psz_name )
335 vlc_mutex_lock( &p_item->lock );
337 free( p_item->psz_name );
338 p_item->psz_name = strdup( psz_name );
340 vlc_mutex_unlock( &p_item->lock );
343 char *input_item_GetURI( input_item_t *p_i )
345 vlc_mutex_lock( &p_i->lock );
347 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
349 vlc_mutex_unlock( &p_i->lock );
352 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
354 vlc_mutex_lock( &p_i->lock );
356 free( p_i->psz_uri );
357 p_i->psz_uri = strdup( psz_uri );
361 if( !p_i->psz_name && p_i->i_type == ITEM_TYPE_FILE )
363 const char *psz_filename = strrchr( p_i->psz_uri, DIR_SEP_CHAR );
364 if( psz_filename && *psz_filename == DIR_SEP_CHAR )
366 if( psz_filename && *psz_filename )
367 p_i->psz_name = strdup( psz_filename );
371 p_i->psz_name = strdup( p_i->psz_uri );
373 vlc_mutex_unlock( &p_i->lock );
376 mtime_t input_item_GetDuration( input_item_t *p_i )
378 vlc_mutex_lock( &p_i->lock );
380 mtime_t i_duration = p_i->i_duration;
382 vlc_mutex_unlock( &p_i->lock );
386 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
388 bool b_send_event = false;
390 vlc_mutex_lock( &p_i->lock );
391 if( p_i->i_duration != i_duration )
393 p_i->i_duration = i_duration;
396 vlc_mutex_unlock( &p_i->lock );
402 event.type = vlc_InputItemDurationChanged;
403 event.u.input_item_duration_changed.new_duration = i_duration;
404 vlc_event_send( &p_i->event_manager, &event );
409 bool input_item_IsPreparsed( input_item_t *p_item )
411 vlc_mutex_lock( &p_item->lock );
412 bool b_preparsed = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_PREPARSED ) != 0 : false;
413 vlc_mutex_unlock( &p_item->lock );
418 bool input_item_IsArtFetched( input_item_t *p_item )
420 vlc_mutex_lock( &p_item->lock );
421 bool b_fetched = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_ART_FETCHED ) != 0 : false;
422 vlc_mutex_unlock( &p_item->lock );
427 static void input_item_Destroy ( gc_object_t *p_gc )
429 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
431 input_item_Clean( p_item );
435 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
438 int err = VLC_SUCCESS;
440 if( psz_option == NULL )
443 vlc_mutex_lock( &p_input->lock );
444 if (flags & VLC_INPUT_OPTION_UNIQUE)
446 for (int i = 0 ; i < p_input->i_options; i++)
447 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
451 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
457 p_input->optflagv = flagv;
458 flagv[p_input->optflagc++] = flags;
460 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
461 p_input->i_options, strdup( psz_option ) );
463 vlc_mutex_unlock( &p_input->lock );
468 * Get a info item from a given category in a given input item.
470 * \param p_i The input item to get info from
471 * \param psz_cat String representing the category for the info
472 * \param psz_name String representing the name of the desired info
473 * \return A pointer to the string with the given info if found, or an
474 * empty string otherwise. The caller should free the returned
477 char *input_item_GetInfo( input_item_t *p_i,
479 const char *psz_name )
481 vlc_mutex_lock( &p_i->lock );
483 for( int i = 0; i< p_i->i_categories; i++ )
485 const info_category_t *p_cat = p_i->pp_categories[i];
487 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
490 for( int j = 0; j < p_cat->i_infos; j++ )
492 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
494 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
495 vlc_mutex_unlock( &p_i->lock );
500 vlc_mutex_unlock( &p_i->lock );
504 static int InputItemVaAddInfo( input_item_t *p_i,
506 const char *psz_name,
507 const char *psz_format, va_list args )
510 info_t *p_info = NULL;
511 info_category_t *p_cat = NULL ;
513 vlc_assert_locked( &p_i->lock );
515 for( i = 0 ; i < p_i->i_categories ; i ++ )
517 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
519 p_cat = p_i->pp_categories[i];
525 if( !(p_cat = malloc( sizeof(*p_cat) )) )
528 p_cat->psz_name = strdup( psz_cat );
531 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
535 for( i = 0; i< p_cat->i_infos; i++ )
537 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
539 p_info = p_cat->pp_infos[i];
546 if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
549 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
550 p_info->psz_name = strdup( psz_name );
554 free( p_info->psz_value );
557 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
558 p_info->psz_value = NULL;
560 return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
563 static int InputItemAddInfo( input_item_t *p_i,
565 const char *psz_name,
566 const char *psz_format, ... )
570 va_start( args, psz_format );
571 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
577 int input_item_AddInfo( input_item_t *p_i,
579 const char *psz_name,
580 const char *psz_format, ... )
584 vlc_mutex_lock( &p_i->lock );
586 va_start( args, psz_format );
587 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
590 vlc_mutex_unlock( &p_i->lock );
597 event.type = vlc_InputItemInfoChanged;
598 vlc_event_send( &p_i->event_manager, &event );
603 int input_item_DelInfo( input_item_t *p_i,
605 const char *psz_name )
607 info_category_t *p_cat = NULL;
611 vlc_mutex_lock( &p_i->lock );
612 for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
614 if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
617 p_cat = p_i->pp_categories[i_cat];
623 vlc_mutex_unlock( &p_i->lock );
629 /* Remove a specific info */
630 for( i = 0; i < p_cat->i_infos; i++ )
632 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
634 free( p_cat->pp_infos[i]->psz_name );
635 free( p_cat->pp_infos[i]->psz_value );
636 free( p_cat->pp_infos[i] );
637 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
641 if( i >= p_cat->i_infos )
643 vlc_mutex_unlock( &p_i->lock );
649 /* Remove the complete categorie */
650 for( i = 0; i < p_cat->i_infos; i++ )
652 free( p_cat->pp_infos[i]->psz_name );
653 free( p_cat->pp_infos[i]->psz_value );
654 free( p_cat->pp_infos[i] );
656 free( p_cat->pp_infos );
657 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
661 free( p_cat->psz_name );
663 vlc_mutex_unlock( &p_i->lock );
667 event.type = vlc_InputItemInfoChanged;
668 vlc_event_send( &p_i->event_manager, &event );
673 void input_item_SetEpg( input_item_t *p_item,
674 const char *psz_epg, const vlc_epg_t *p_epg )
676 input_item_DelInfo( p_item, psz_epg, NULL );
678 #ifdef HAVE_LOCALTIME_R
679 vlc_mutex_lock( &p_item->lock );
680 for( int i = 0; i < p_epg->i_event; i++ )
682 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
683 time_t t_start = (time_t)p_evt->i_start;
687 localtime_r( &t_start, &tm_start );
689 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
690 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
691 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
692 if( p_evt->psz_short_description || p_evt->psz_description )
693 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
695 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
696 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
698 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
700 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
702 vlc_mutex_unlock( &p_item->lock );
704 if( p_epg->i_event > 0 )
708 event.type = vlc_InputItemInfoChanged;
709 vlc_event_send( &p_item->event_manager, &event );
715 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
716 const char *psz_name,
718 const char *const *ppsz_options,
719 unsigned i_option_flags,
722 return input_item_NewWithType( p_obj, psz_uri, psz_name,
723 i_options, ppsz_options, i_option_flags,
724 i_duration, ITEM_TYPE_UNKNOWN );
728 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
729 const char *psz_name,
731 const char *const *ppsz_options,
732 unsigned i_option_flags,
736 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
737 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
739 input_item_t* p_input = malloc( sizeof(input_item_t ) );
743 input_item_Init( p_obj, p_input );
744 vlc_gc_init( p_input, input_item_Destroy );
746 vlc_mutex_lock( &input_id_lock );
747 p_input->i_id = ++priv->i_last_input_id;
748 vlc_mutex_unlock( &input_id_lock );
750 p_input->b_fixed_name = false;
752 p_input->i_type = i_type;
753 p_input->b_prefers_tree = false;
756 input_item_SetURI( p_input, psz_uri );
758 if( i_type != ITEM_TYPE_UNKNOWN )
759 p_input->i_type = i_type;
762 input_item_SetName( p_input, psz_name );
764 p_input->i_duration = i_duration;
766 for( int i = 0; i < i_options; i++ )
767 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
771 /* Guess the type of the item using the beginning of the mrl */
772 static void GuessType( input_item_t *p_item)
775 static struct { const char *psz_search; int i_type; } types_array[] =
777 { "http", ITEM_TYPE_NET },
778 { "dvd", ITEM_TYPE_DISC },
779 { "cdda", ITEM_TYPE_CDDA },
780 { "mms", ITEM_TYPE_NET },
781 { "rtsp", ITEM_TYPE_NET },
782 { "udp", ITEM_TYPE_NET },
783 { "rtp", ITEM_TYPE_NET },
784 { "vcd", ITEM_TYPE_DISC },
785 { "v4l", ITEM_TYPE_CARD },
786 { "dshow", ITEM_TYPE_CARD },
787 { "pvr", ITEM_TYPE_CARD },
788 { "dvb", ITEM_TYPE_CARD },
789 { "qpsk", ITEM_TYPE_CARD },
790 { "sdp", ITEM_TYPE_NET },
791 { "ftp", ITEM_TYPE_NET },
792 { "smb", ITEM_TYPE_NET },
796 if( !p_item->psz_uri || !strstr( p_item->psz_uri, "://" ) )
798 p_item->i_type = ITEM_TYPE_FILE;
802 for( i = 0; types_array[i].psz_search != NULL; i++ )
804 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
805 strlen( types_array[i].psz_search ) ) )
807 p_item->i_type = types_array[i].i_type;
811 p_item->i_type = ITEM_TYPE_FILE;