1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #include <vlc/input.h>
29 #include "vlc_playlist.h"
32 #include "../../modules/demux/util/sub.h"
34 struct input_thread_sys_t
38 subtitle_demux_t **sub;
42 static void UpdateBookmarksOption( input_thread_t * );
44 /****************************************************************************
46 ****************************************************************************/
48 * Control function for inputs.
49 * \param p_input input handle
50 * \param i_query query type
51 * \return VLC_SUCESS if ok
53 int input_Control( input_thread_t *p_input, int i_query, ... )
58 va_start( args, i_query );
59 i_result = input_vaControl( p_input, i_query, args );
65 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
68 seekpoint_t *p_bkmk, ***ppp_bkmk;
71 vlc_value_t val, text;
72 char *psz_option, *psz_value;
74 vlc_mutex_lock( &p_input->stream.stream_lock );
77 case INPUT_ADD_OPTION:
79 psz_option = (char *)va_arg( args, char * );
80 psz_value = (char *)va_arg( args, char * );
83 vlc_mutex_lock( &p_input->p_item->lock );
84 /* Check if option already exists */
85 for( i = 0; i < p_input->p_item->i_options; i++ )
87 if( !strncmp( p_input->p_item->ppsz_options[i], psz_option,
88 strlen( psz_option ) ) &&
89 p_input->p_item->ppsz_options[i][strlen(psz_option)]
92 free( p_input->p_item->ppsz_options[i] );
96 if( i == p_input->p_item->i_options )
98 p_input->p_item->i_options++;
99 p_input->p_item->ppsz_options =
100 realloc( p_input->p_item->ppsz_options,
101 p_input->p_item->i_options * sizeof(char **) );
104 asprintf( &p_input->p_item->ppsz_options[i],
105 "%s=%s", psz_option, psz_value ) ;
106 vlc_mutex_unlock( &p_input->p_item->lock );
114 char *psz_name = (char *)va_arg( args, char * );
115 i_ret = VLC_EGENERIC;
116 if( !psz_name ) break;
117 vlc_mutex_lock( &p_input->p_item->lock );
118 if( p_input->p_item->psz_name ) free( p_input->p_item->psz_name );
119 p_input->p_item->psz_name = strdup( psz_name );
120 vlc_mutex_unlock( &p_input->p_item->lock );
123 /* Notify playlist */
126 playlist_t *p_playlist =
127 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
131 val.i_int = p_playlist->i_index;
132 var_Set( p_playlist, "item-change", val );
133 vlc_object_release( p_playlist );
141 char *psz_cat = (char *)va_arg( args, char * );
142 char *psz_name = (char *)va_arg( args, char * );
143 char *psz_format = (char *)va_arg( args, char * );
145 info_category_t *p_cat;
149 i_ret = VLC_EGENERIC;
151 vlc_mutex_lock( &p_input->p_item->lock );
152 for( i = 0; i < p_input->p_item->i_categories; i++ )
154 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
159 if( i == p_input->p_item->i_categories )
161 p_cat = malloc( sizeof( info_category_t ) );
163 p_cat->psz_name = strdup( psz_cat );
165 p_cat->pp_infos = NULL;
166 INSERT_ELEM( p_input->p_item->pp_categories,
167 p_input->p_item->i_categories,
168 p_input->p_item->i_categories, p_cat );
171 p_cat = p_input->p_item->pp_categories[i];
173 for( i = 0; i < p_cat->i_infos; i++ )
175 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
177 if( p_cat->pp_infos[i]->psz_value )
178 free( p_cat->pp_infos[i]->psz_value );
183 if( i == p_cat->i_infos )
185 p_info = malloc( sizeof( info_t ) );
187 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
188 p_cat->i_infos, p_info );
189 p_info->psz_name = strdup( psz_name );
192 p_info = p_cat->pp_infos[i];
193 vasprintf( &p_info->psz_value, psz_format, args );
195 vlc_mutex_unlock( &p_input->p_item->lock );
199 /* Notify playlist */
202 playlist_t *p_playlist =
203 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
207 val.i_int = p_playlist->i_index;
208 var_Set( p_playlist, "item-change", val );
209 vlc_object_release( p_playlist );
217 char *psz_cat = (char *)va_arg( args, char * );
218 char *psz_name = (char *)va_arg( args, char * );
219 char **ppsz_value = (char **)va_arg( args, char ** );
222 i_ret = VLC_EGENERIC;
225 vlc_mutex_lock( &p_input->p_item->lock );
226 for( i = 0; i < p_input->p_item->i_categories; i++ )
228 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
233 if( i != p_input->p_item->i_categories )
235 info_category_t *p_cat;
236 p_cat = p_input->p_item->pp_categories[i];
238 for( i = 0; i < p_cat->i_infos; i++ )
240 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
242 if( p_cat->pp_infos[i]->psz_value )
244 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
251 vlc_mutex_unlock( &p_input->p_item->lock );
255 case INPUT_ADD_BOOKMARK:
256 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
257 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
258 if( !p_bkmk->psz_name )
260 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
261 p_input->i_bookmarks );
263 TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
265 /* Reflect the changes on the object var */
266 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
269 for( i = 0; i < p_input->i_bookmarks; i++ )
272 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
273 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
278 UpdateBookmarksOption( p_input );
283 case INPUT_DEL_BOOKMARK:
284 i_bkmk = (int)va_arg( args, int );
285 if( i_bkmk < p_input->i_bookmarks )
288 p_bkmk = p_input->pp_bookmarks[i_bkmk];
289 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
291 vlc_seekpoint_Delete( p_bkmk );
293 /* Reflect the changes on the object var */
294 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
295 for( i = 0; i < p_input->i_bookmarks; i++ )
298 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
299 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
303 UpdateBookmarksOption( p_input );
307 else i_ret = VLC_EGENERIC;
310 case INPUT_GET_BOOKMARKS:
311 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
312 pi_bkmk = (int *)va_arg( args, int * );
313 if( p_input->i_bookmarks )
317 *pi_bkmk = p_input->i_bookmarks;
318 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
319 p_input->i_bookmarks );
320 for( i = 0; i < p_input->i_bookmarks; i++ )
323 vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
331 i_ret = VLC_EGENERIC;
335 case INPUT_CLEAR_BOOKMARKS:
336 if( p_input->i_bookmarks )
340 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
342 p_bkmk = p_input->pp_bookmarks[i];
343 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
345 vlc_seekpoint_Delete( p_bkmk );
347 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
350 UpdateBookmarksOption( p_input );
355 case INPUT_SET_BOOKMARK:
356 i_bkmk = (int)va_arg( args, int );
357 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
360 vlc_mutex_unlock( &p_input->stream.stream_lock );
361 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
362 ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
363 !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
365 pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
366 (double)p_input->stream.p_selected_area->i_size;
367 i_ret = var_Set( p_input, "position", pos );
369 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
371 pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
372 i_ret = var_Set( p_input, "time", pos );
374 vlc_mutex_lock( &p_input->stream.stream_lock );
378 i_ret = VLC_EGENERIC;
382 case INPUT_GET_SUBDELAY:
383 pi = (int*)va_arg( args, int *);
384 /* We work on the first subtitle */
385 if( p_input->p_sys != NULL )
387 if( p_input->p_sys->i_sub > 0 )
389 i_ret = var_Get( (vlc_object_t *)p_input->p_sys->sub[0],
395 msg_Dbg( p_input,"no subtitle track");
396 i_ret = VLC_EGENERIC;
401 i_ret = VLC_EGENERIC;
405 case INPUT_SET_SUBDELAY:
406 i = (int)va_arg( args, int );
407 /* We work on the first subtitle */
410 if( p_input->p_sys->i_sub > 0 )
413 i_ret = var_Set( (vlc_object_t *)p_input->p_sys->sub[0],
418 msg_Dbg( p_input,"no subtitle track");
419 i_ret = VLC_EGENERIC;
424 i_ret = VLC_EGENERIC;
429 msg_Err( p_input, "unknown query in input_vaControl" );
430 i_ret = VLC_EGENERIC;
433 vlc_mutex_unlock( &p_input->stream.stream_lock );
438 static void UpdateBookmarksOption( input_thread_t *p_input )
441 char *psz_value = NULL, *psz_next = NULL;
443 vlc_mutex_unlock( &p_input->stream.stream_lock );
445 for( i = 0; i < p_input->i_bookmarks; i++ )
447 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
448 p_input->pp_bookmarks[i]->psz_name,
449 p_input->pp_bookmarks[i]->i_byte_offset,
450 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
451 i_len += strlen( psz_value );
454 for( i = 0; i < p_input->i_bookmarks; i++ )
456 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmarks );
458 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
459 p_input->pp_bookmarks[i]->psz_name,
460 p_input->pp_bookmarks[i]->i_byte_offset,
461 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
463 psz_next += strlen( psz_next );
464 if( i < p_input->i_bookmarks - 1)
466 *psz_next = ','; psz_next++;
469 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
470 psz_value ? psz_value : "" );
472 vlc_mutex_lock( &p_input->stream.stream_lock );