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 vlc_mutex_unlock( &p_input->stream.stream_lock );
133 var_Set( p_playlist, "item-change", val );
134 vlc_mutex_lock( &p_input->stream.stream_lock );
135 vlc_object_release( p_playlist );
143 char *psz_cat = (char *)va_arg( args, char * );
144 char *psz_name = (char *)va_arg( args, char * );
145 char *psz_format = (char *)va_arg( args, char * );
147 info_category_t *p_cat;
151 i_ret = VLC_EGENERIC;
153 vlc_mutex_lock( &p_input->p_item->lock );
154 for( i = 0; i < p_input->p_item->i_categories; i++ )
156 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
161 if( i == p_input->p_item->i_categories )
163 p_cat = malloc( sizeof( info_category_t ) );
165 p_cat->psz_name = strdup( psz_cat );
167 p_cat->pp_infos = NULL;
168 INSERT_ELEM( p_input->p_item->pp_categories,
169 p_input->p_item->i_categories,
170 p_input->p_item->i_categories, p_cat );
173 p_cat = p_input->p_item->pp_categories[i];
175 for( i = 0; i < p_cat->i_infos; i++ )
177 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
179 if( p_cat->pp_infos[i]->psz_value )
180 free( p_cat->pp_infos[i]->psz_value );
185 if( i == p_cat->i_infos )
187 p_info = malloc( sizeof( info_t ) );
189 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
190 p_cat->i_infos, p_info );
191 p_info->psz_name = strdup( psz_name );
194 p_info = p_cat->pp_infos[i];
195 vasprintf( &p_info->psz_value, psz_format, args );
197 vlc_mutex_unlock( &p_input->p_item->lock );
201 /* Notify playlist */
204 playlist_t *p_playlist =
205 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
209 val.i_int = p_playlist->i_index;
210 vlc_mutex_unlock( &p_input->stream.stream_lock );
211 var_Set( p_playlist, "item-change", val );
212 vlc_mutex_lock( &p_input->stream.stream_lock );
213 vlc_object_release( p_playlist );
221 char *psz_cat = (char *)va_arg( args, char * );
222 char *psz_name = (char *)va_arg( args, char * );
223 char **ppsz_value = (char **)va_arg( args, char ** );
226 i_ret = VLC_EGENERIC;
229 vlc_mutex_lock( &p_input->p_item->lock );
230 for( i = 0; i < p_input->p_item->i_categories; i++ )
232 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
237 if( i != p_input->p_item->i_categories )
239 info_category_t *p_cat;
240 p_cat = p_input->p_item->pp_categories[i];
242 for( i = 0; i < p_cat->i_infos; i++ )
244 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
246 if( p_cat->pp_infos[i]->psz_value )
248 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
255 vlc_mutex_unlock( &p_input->p_item->lock );
259 case INPUT_ADD_BOOKMARK:
260 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
261 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
262 if( !p_bkmk->psz_name )
264 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
265 p_input->i_bookmarks );
267 TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
269 /* Reflect the changes on the object var */
270 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
273 for( i = 0; i < p_input->i_bookmarks; i++ )
276 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
277 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
282 UpdateBookmarksOption( p_input );
287 case INPUT_DEL_BOOKMARK:
288 i_bkmk = (int)va_arg( args, int );
289 if( i_bkmk < p_input->i_bookmarks )
292 p_bkmk = p_input->pp_bookmarks[i_bkmk];
293 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
295 vlc_seekpoint_Delete( p_bkmk );
297 /* Reflect the changes on the object var */
298 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
299 for( i = 0; i < p_input->i_bookmarks; i++ )
302 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
303 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
307 UpdateBookmarksOption( p_input );
311 else i_ret = VLC_EGENERIC;
314 case INPUT_GET_BOOKMARKS:
315 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
316 pi_bkmk = (int *)va_arg( args, int * );
317 if( p_input->i_bookmarks )
321 *pi_bkmk = p_input->i_bookmarks;
322 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
323 p_input->i_bookmarks );
324 for( i = 0; i < p_input->i_bookmarks; i++ )
327 vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
335 i_ret = VLC_EGENERIC;
339 case INPUT_CLEAR_BOOKMARKS:
340 if( p_input->i_bookmarks )
344 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
346 p_bkmk = p_input->pp_bookmarks[i];
347 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
349 vlc_seekpoint_Delete( p_bkmk );
351 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
354 UpdateBookmarksOption( p_input );
359 case INPUT_SET_BOOKMARK:
360 i_bkmk = (int)va_arg( args, int );
361 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
364 vlc_mutex_unlock( &p_input->stream.stream_lock );
365 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
366 ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
367 !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
369 pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
370 (double)p_input->stream.p_selected_area->i_size;
371 i_ret = var_Set( p_input, "position", pos );
373 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
375 pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
376 i_ret = var_Set( p_input, "time", pos );
378 vlc_mutex_lock( &p_input->stream.stream_lock );
382 i_ret = VLC_EGENERIC;
386 case INPUT_GET_SUBDELAY:
387 pi = (int*)va_arg( args, int *);
388 /* We work on the first subtitle */
389 if( p_input->p_sys != NULL )
391 if( p_input->p_sys->i_sub > 0 )
393 i_ret = var_Get( (vlc_object_t *)p_input->p_sys->sub[0],
399 msg_Dbg( p_input,"no subtitle track");
400 i_ret = VLC_EGENERIC;
405 i_ret = VLC_EGENERIC;
409 case INPUT_SET_SUBDELAY:
410 i = (int)va_arg( args, int );
411 /* We work on the first subtitle */
414 if( p_input->p_sys->i_sub > 0 )
417 i_ret = var_Set( (vlc_object_t *)p_input->p_sys->sub[0],
422 msg_Dbg( p_input,"no subtitle track");
423 i_ret = VLC_EGENERIC;
428 i_ret = VLC_EGENERIC;
433 msg_Err( p_input, "unknown query in input_vaControl" );
434 i_ret = VLC_EGENERIC;
437 vlc_mutex_unlock( &p_input->stream.stream_lock );
442 static void UpdateBookmarksOption( input_thread_t *p_input )
445 char *psz_value = NULL, *psz_next = NULL;
447 vlc_mutex_unlock( &p_input->stream.stream_lock );
449 for( i = 0; i < p_input->i_bookmarks; i++ )
451 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
452 p_input->pp_bookmarks[i]->psz_name,
453 p_input->pp_bookmarks[i]->i_byte_offset,
454 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
455 i_len += strlen( psz_value );
458 for( i = 0; i < p_input->i_bookmarks; i++ )
460 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmarks );
462 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
463 p_input->pp_bookmarks[i]->psz_name,
464 p_input->pp_bookmarks[i]->i_byte_offset,
465 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
467 psz_next += strlen( psz_next );
468 if( i < p_input->i_bookmarks - 1)
470 *psz_next = ','; psz_next++;
473 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
474 psz_value ? psz_value : "" );
476 vlc_mutex_lock( &p_input->stream.stream_lock );