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;
72 vlc_value_t val, text;
73 char *psz_option, *psz_value;
75 vlc_mutex_lock( &p_input->stream.stream_lock );
78 case INPUT_ADD_OPTION:
80 psz_option = (char *)va_arg( args, char * );
81 psz_value = (char *)va_arg( args, char * );
84 vlc_mutex_lock( &p_input->p_item->lock );
85 /* Check if option already exists */
86 for( i = 0; i < p_input->p_item->i_options; i++ )
88 if( !strncmp( p_input->p_item->ppsz_options[i], psz_option,
89 strlen( psz_option ) ) &&
90 p_input->p_item->ppsz_options[i][strlen(psz_option)]
93 free( p_input->p_item->ppsz_options[i] );
97 if( i == p_input->p_item->i_options )
99 p_input->p_item->i_options++;
100 p_input->p_item->ppsz_options =
101 realloc( p_input->p_item->ppsz_options,
102 p_input->p_item->i_options * sizeof(char **) );
105 asprintf( &p_input->p_item->ppsz_options[i],
106 "%s=%s", psz_option, psz_value ) ;
107 vlc_mutex_unlock( &p_input->p_item->lock );
115 char *psz_name = (char *)va_arg( args, char * );
116 i_ret = VLC_EGENERIC;
117 if( !psz_name ) break;
118 vlc_mutex_lock( &p_input->p_item->lock );
119 if( p_input->p_item->psz_name ) free( p_input->p_item->psz_name );
120 p_input->p_item->psz_name = strdup( psz_name );
121 vlc_mutex_unlock( &p_input->p_item->lock );
124 /* Notify playlist */
127 playlist_t *p_playlist =
128 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
132 val.i_int = p_playlist->i_index;
133 vlc_mutex_unlock( &p_input->stream.stream_lock );
134 var_Set( p_playlist, "item-change", val );
135 vlc_mutex_lock( &p_input->stream.stream_lock );
136 vlc_object_release( p_playlist );
144 char *psz_cat = (char *)va_arg( args, char * );
145 char *psz_name = (char *)va_arg( args, char * );
146 char *psz_format = (char *)va_arg( args, char * );
148 info_category_t *p_cat;
152 i_ret = VLC_EGENERIC;
154 vlc_mutex_lock( &p_input->p_item->lock );
155 for( i = 0; i < p_input->p_item->i_categories; i++ )
157 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
162 if( i == p_input->p_item->i_categories )
164 p_cat = malloc( sizeof( info_category_t ) );
166 p_cat->psz_name = strdup( psz_cat );
168 p_cat->pp_infos = NULL;
169 INSERT_ELEM( p_input->p_item->pp_categories,
170 p_input->p_item->i_categories,
171 p_input->p_item->i_categories, p_cat );
174 p_cat = p_input->p_item->pp_categories[i];
176 for( i = 0; i < p_cat->i_infos; i++ )
178 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
180 if( p_cat->pp_infos[i]->psz_value )
181 free( p_cat->pp_infos[i]->psz_value );
186 if( i == p_cat->i_infos )
188 p_info = malloc( sizeof( info_t ) );
190 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
191 p_cat->i_infos, p_info );
192 p_info->psz_name = strdup( psz_name );
195 p_info = p_cat->pp_infos[i];
196 vasprintf( &p_info->psz_value, psz_format, args );
198 vlc_mutex_unlock( &p_input->p_item->lock );
202 /* Notify playlist */
205 playlist_t *p_playlist =
206 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
210 val.i_int = p_playlist->i_index;
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
212 var_Set( p_playlist, "item-change", val );
213 vlc_mutex_lock( &p_input->stream.stream_lock );
214 vlc_object_release( p_playlist );
222 char *psz_cat = (char *)va_arg( args, char * );
223 char *psz_name = (char *)va_arg( args, char * );
224 char **ppsz_value = (char **)va_arg( args, char ** );
227 i_ret = VLC_EGENERIC;
230 vlc_mutex_lock( &p_input->p_item->lock );
231 for( i = 0; i < p_input->p_item->i_categories; i++ )
233 if( !strcmp( p_input->p_item->pp_categories[i]->psz_name,
238 if( i != p_input->p_item->i_categories )
240 info_category_t *p_cat;
241 p_cat = p_input->p_item->pp_categories[i];
243 for( i = 0; i < p_cat->i_infos; i++ )
245 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
247 if( p_cat->pp_infos[i]->psz_value )
249 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
256 vlc_mutex_unlock( &p_input->p_item->lock );
260 case INPUT_ADD_BOOKMARK:
261 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
262 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
263 if( !p_bkmk->psz_name )
265 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
266 p_input->i_bookmarks );
268 TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
270 /* Reflect the changes on the object var */
271 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
274 for( i = 0; i < p_input->i_bookmarks; i++ )
277 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
278 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
283 UpdateBookmarksOption( p_input );
288 case INPUT_CHANGE_BOOKMARK:
289 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
290 i_bkmk = (int)va_arg( args, int );
292 if( i_bkmk < p_input->i_bookmarks )
294 p_input->pp_bookmarks[i_bkmk] = p_bkmk;
296 /* Reflect the changes on the object var */
297 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
298 for( i = 0; i < p_input->i_bookmarks; i++ )
301 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
302 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
306 UpdateBookmarksOption( p_input );
311 case INPUT_DEL_BOOKMARK:
312 i_bkmk = (int)va_arg( args, int );
313 if( i_bkmk < p_input->i_bookmarks )
316 p_bkmk = p_input->pp_bookmarks[i_bkmk];
317 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
319 vlc_seekpoint_Delete( p_bkmk );
321 /* Reflect the changes on the object var */
322 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
323 for( i = 0; i < p_input->i_bookmarks; i++ )
326 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
327 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
331 UpdateBookmarksOption( p_input );
335 else i_ret = VLC_EGENERIC;
338 case INPUT_GET_BOOKMARKS:
339 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
340 pi_bkmk = (int *)va_arg( args, int * );
341 if( p_input->i_bookmarks )
345 *pi_bkmk = p_input->i_bookmarks;
346 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
347 p_input->i_bookmarks );
348 for( i = 0; i < p_input->i_bookmarks; i++ )
351 vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
359 i_ret = VLC_EGENERIC;
363 case INPUT_CLEAR_BOOKMARKS:
364 if( p_input->i_bookmarks )
368 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
370 p_bkmk = p_input->pp_bookmarks[i];
371 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
373 vlc_seekpoint_Delete( p_bkmk );
375 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
378 UpdateBookmarksOption( p_input );
383 case INPUT_SET_BOOKMARK:
384 i_bkmk = (int)va_arg( args, int );
385 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
388 vlc_mutex_unlock( &p_input->stream.stream_lock );
389 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
390 ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
391 !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
393 pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
394 (double)p_input->stream.p_selected_area->i_size;
395 i_ret = var_Set( p_input, "position", pos );
397 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
399 pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
400 i_ret = var_Set( p_input, "time", pos );
402 vlc_mutex_lock( &p_input->stream.stream_lock );
406 i_ret = VLC_EGENERIC;
410 case INPUT_GET_SUBDELAY:
411 pi = (int*)va_arg( args, int *);
412 /* We work on the first subtitle */
413 if( p_input->p_sys != NULL )
415 if( p_input->p_sys->i_sub > 0 )
417 i_ret = var_Get( (vlc_object_t *)p_input->p_sys->sub[0],
423 msg_Dbg( p_input,"no subtitle track");
424 i_ret = VLC_EGENERIC;
429 i_ret = VLC_EGENERIC;
433 case INPUT_SET_SUBDELAY:
434 i = (int)va_arg( args, int );
435 /* We work on the first subtitle */
438 if( p_input->p_sys->i_sub > 0 )
441 i_ret = var_Set( (vlc_object_t *)p_input->p_sys->sub[0],
446 msg_Dbg( p_input,"no subtitle track");
447 i_ret = VLC_EGENERIC;
452 i_ret = VLC_EGENERIC;
457 msg_Err( p_input, "unknown query in input_vaControl" );
458 i_ret = VLC_EGENERIC;
461 vlc_mutex_unlock( &p_input->stream.stream_lock );
466 static void UpdateBookmarksOption( input_thread_t *p_input )
469 char *psz_value = NULL, *psz_next = NULL;
471 vlc_mutex_unlock( &p_input->stream.stream_lock );
473 for( i = 0; i < p_input->i_bookmarks; i++ )
475 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
476 p_input->pp_bookmarks[i]->psz_name,
477 p_input->pp_bookmarks[i]->i_byte_offset,
478 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
479 i_len += strlen( psz_value );
482 for( i = 0; i < p_input->i_bookmarks; i++ )
484 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmarks );
486 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
487 p_input->pp_bookmarks[i]->psz_name,
488 p_input->pp_bookmarks[i]->i_byte_offset,
489 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
491 psz_next += strlen( psz_next );
492 if( i < p_input->i_bookmarks - 1)
494 *psz_next = ','; psz_next++;
497 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
498 psz_value ? psz_value : "" );
500 vlc_mutex_lock( &p_input->stream.stream_lock );