1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN (Centrale Réseaux) and its contributors
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 *****************************************************************************/
26 #include <vlc/input.h>
28 #include "input_internal.h"
29 #include "vlc_playlist.h"
32 static void UpdateBookmarksOption( input_thread_t * );
33 static void NotifyPlaylist( input_thread_t * );
35 /****************************************************************************
37 ****************************************************************************/
39 * Control function for inputs.
40 * \param p_input input handle
41 * \param i_query query type
42 * \return VLC_SUCCESS if ok
44 int input_Control( input_thread_t *p_input, int i_query, ... )
49 va_start( args, i_query );
50 i_result = input_vaControl( p_input, i_query, args );
56 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
58 seekpoint_t *p_bkmk, ***ppp_bkmk;
71 case INPUT_GET_POSITION:
72 pf = (double*)va_arg( args, double * );
73 *pf = var_GetFloat( p_input, "position" );
76 case INPUT_SET_POSITION:
77 f = (double)va_arg( args, double );
78 return var_SetFloat( p_input, "position", f );
80 case INPUT_GET_LENGTH:
81 pi_64 = (int64_t*)va_arg( args, int64_t * );
82 *pi_64 = var_GetTime( p_input, "length" );
86 pi_64 = (int64_t*)va_arg( args, int64_t * );
87 *pi_64 = var_GetTime( p_input, "time" );
91 i_64 = (int64_t)va_arg( args, int64_t );
92 return var_SetTime( p_input, "time", i_64 );
95 pi_int = (int*)va_arg( args, int * );
96 *pi_int = var_GetInteger( p_input, "rate" );
100 i_int = (int)va_arg( args, int );
101 return var_SetInteger( p_input, "rate", i_int );
103 case INPUT_GET_STATE:
104 pi_int = (int*)va_arg( args, int * );
105 *pi_int = var_GetInteger( p_input, "state" );
108 case INPUT_SET_STATE:
109 i_int = (int)va_arg( args, int );
110 return var_SetInteger( p_input, "state", i_int );
112 case INPUT_GET_AUDIO_DELAY:
113 pi_64 = (int64_t*)va_arg( args, int64_t * );
114 *pi_64 = var_GetTime( p_input, "audio-delay" );
117 case INPUT_GET_SPU_DELAY:
118 pi_64 = (int64_t*)va_arg( args, int64_t * );
119 *pi_64 = var_GetTime( p_input, "spu-delay" );
122 case INPUT_SET_AUDIO_DELAY:
123 i_64 = (int64_t)va_arg( args, int64_t );
124 return var_SetTime( p_input, "audio-delay", i_64 );
126 case INPUT_SET_SPU_DELAY:
127 i_64 = (int64_t)va_arg( args, int64_t );
128 return var_SetTime( p_input, "spu-delay", i_64 );
132 /* FIXME : Impossible to use vlc_input_item_AddInfo because of
133 * the ... problem ? */
134 char *psz_cat = (char *)va_arg( args, char * );
135 char *psz_name = (char *)va_arg( args, char * );
136 char *psz_format = (char *)va_arg( args, char * );
138 info_category_t *p_cat;
142 vlc_mutex_lock( &p_input->input.p_item->lock );
143 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
145 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
149 if( i == p_input->input.p_item->i_categories )
151 p_cat = malloc( sizeof( info_category_t ) );
154 vlc_mutex_lock( &p_input->input.p_item->lock );
157 p_cat->psz_name = strdup( psz_cat );
159 p_cat->pp_infos = NULL;
160 INSERT_ELEM( p_input->input.p_item->pp_categories,
161 p_input->input.p_item->i_categories,
162 p_input->input.p_item->i_categories, p_cat );
165 p_cat = p_input->input.p_item->pp_categories[i];
167 for( i = 0; i < p_cat->i_infos; i++ )
169 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
171 if( p_cat->pp_infos[i]->psz_value )
172 free( p_cat->pp_infos[i]->psz_value );
177 if( i == p_cat->i_infos )
179 p_info = malloc( sizeof( info_t ) );
182 vlc_mutex_lock( &p_input->input.p_item->lock );
186 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
187 p_cat->i_infos, p_info );
188 p_info->psz_name = strdup( psz_name );
191 p_info = p_cat->pp_infos[i];
192 vasprintf( &p_info->psz_value, psz_format, args );
194 vlc_mutex_unlock( &p_input->input.p_item->lock );
196 NotifyPlaylist( p_input );
201 char *psz_cat = (char *)va_arg( args, char * );
202 char *psz_name = (char *)va_arg( args, char * );
204 info_category_t *p_cat = NULL;
207 vlc_mutex_lock( &p_input->input.p_item->lock );
208 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
210 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
213 p_cat = p_input->input.p_item->pp_categories[i];
219 vlc_mutex_unlock( &p_input->input.p_item->lock );
223 for( i = 0; i < p_cat->i_infos; i++ )
225 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
227 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
231 vlc_mutex_unlock( &p_input->input.p_item->lock );
233 if( i >= p_cat->i_infos )
236 NotifyPlaylist( p_input );
243 char *psz_cat = (char *)va_arg( args, char * );
244 char *psz_name = (char *)va_arg( args, char * );
245 char **ppsz_value = (char **)va_arg( args, char ** );
246 int i_ret = VLC_EGENERIC;
249 *ppsz_value = vlc_input_item_GetInfo( p_input->input.p_item,
256 char *psz_name = (char *)va_arg( args, char * );
258 if( !psz_name ) return VLC_EGENERIC;
260 vlc_mutex_lock( &p_input->input.p_item->lock );
261 if( p_input->input.p_item->psz_name )
262 free( p_input->input.p_item->psz_name );
263 p_input->input.p_item->psz_name = strdup( psz_name );
264 vlc_mutex_unlock( &p_input->input.p_item->lock );
266 NotifyPlaylist( p_input );
271 case INPUT_ADD_BOOKMARK:
272 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
273 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
275 vlc_mutex_lock( &p_input->input.p_item->lock );
276 if( !p_bkmk->psz_name )
278 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
279 p_input->i_bookmark );
282 TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
284 /* Reflect the changes on the object var */
285 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
287 vlc_value_t val, text;
290 for( i = 0; i < p_input->i_bookmark; i++ )
293 text.psz_string = p_input->bookmark[i]->psz_name;
294 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
298 vlc_mutex_unlock( &p_input->input.p_item->lock );
300 UpdateBookmarksOption( p_input );
304 case INPUT_CHANGE_BOOKMARK:
305 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
306 i_bkmk = (int)va_arg( args, int );
308 vlc_mutex_lock( &p_input->input.p_item->lock );
309 if( i_bkmk < p_input->i_bookmark )
311 vlc_value_t val, text;
314 p_input->bookmark[i_bkmk] = p_bkmk;
316 /* Reflect the changes on the object var */
317 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
318 for( i = 0; i < p_input->i_bookmark; i++ )
321 text.psz_string = p_input->bookmark[i]->psz_name;
322 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
326 vlc_mutex_unlock( &p_input->input.p_item->lock );
328 UpdateBookmarksOption( p_input );
332 case INPUT_DEL_BOOKMARK:
333 i_bkmk = (int)va_arg( args, int );
335 vlc_mutex_lock( &p_input->input.p_item->lock );
336 if( i_bkmk < p_input->i_bookmark )
338 vlc_value_t val, text;
341 p_bkmk = p_input->bookmark[i_bkmk];
342 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
344 vlc_seekpoint_Delete( p_bkmk );
346 /* Reflect the changes on the object var */
347 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
348 for( i = 0; i < p_input->i_bookmark; i++ )
351 text.psz_string = p_input->bookmark[i]->psz_name;
352 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
355 vlc_mutex_unlock( &p_input->input.p_item->lock );
357 UpdateBookmarksOption( p_input );
361 vlc_mutex_unlock( &p_input->input.p_item->lock );
365 case INPUT_GET_BOOKMARKS:
366 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
367 pi_bkmk = (int *)va_arg( args, int * );
369 vlc_mutex_lock( &p_input->input.p_item->lock );
370 if( p_input->i_bookmark )
374 *pi_bkmk = p_input->i_bookmark;
375 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
376 p_input->i_bookmark );
377 for( i = 0; i < p_input->i_bookmark; i++ )
380 vlc_seekpoint_Duplicate(p_input->bookmark[i]);
383 vlc_mutex_unlock( &p_input->input.p_item->lock );
391 vlc_mutex_unlock( &p_input->input.p_item->lock );
396 case INPUT_CLEAR_BOOKMARKS:
398 vlc_mutex_lock( &p_input->input.p_item->lock );
399 if( p_input->i_bookmark )
403 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
405 p_bkmk = p_input->bookmark[i];
406 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
408 vlc_seekpoint_Delete( p_bkmk );
410 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
412 vlc_mutex_unlock( &p_input->input.p_item->lock );
414 UpdateBookmarksOption( p_input );
418 case INPUT_SET_BOOKMARK:
419 i_bkmk = (int)va_arg( args, int );
421 vlc_mutex_lock( &p_input->input.p_item->lock );
422 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
427 if( p_input->bookmark[i_bkmk]->i_time_offset != -1 )
429 pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
430 i_ret = var_Set( p_input, "time", pos );
432 else if( p_input->bookmark[i_bkmk]->i_byte_offset != -1 )
434 // don't crash on bookmarks in live streams
435 if( stream_Size( p_input->input.p_stream ) == 0 )
437 vlc_mutex_unlock( &p_input->input.p_item->lock );
440 pos.f_float = !p_input->input.p_stream ? 0 :
441 p_input->bookmark[i_bkmk]->i_byte_offset /
442 stream_Size( p_input->input.p_stream );
443 i_ret = var_Set( p_input, "position", pos );
448 i_ret = var_Set( p_input, "position", pos );
451 vlc_mutex_unlock( &p_input->input.p_item->lock );
456 vlc_mutex_unlock( &p_input->input.p_item->lock );
462 case INPUT_ADD_OPTION:
464 char *psz_option = (char *)va_arg( args, char * );
465 char *psz_value = (char *)va_arg( args, char * );
468 vlc_mutex_lock( &p_input->input.p_item->lock );
469 /* Check if option already exists */
470 for( i = 0; i < p_input->input.p_item->i_options; i++ )
472 if( !strncmp( p_input->input.p_item->ppsz_options[i],
473 psz_option, strlen( psz_option ) ) &&
474 p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
477 free( p_input->input.p_item->ppsz_options[i] );
481 if( i == p_input->input.p_item->i_options )
483 p_input->input.p_item->i_options++;
484 p_input->input.p_item->ppsz_options =
485 realloc( p_input->input.p_item->ppsz_options,
486 p_input->input.p_item->i_options *
490 asprintf( &p_input->input.p_item->ppsz_options[i],
491 "%s=%s", psz_option, psz_value ) ;
492 vlc_mutex_unlock( &p_input->input.p_item->lock );
497 case INPUT_GET_BYTE_POSITION:
498 pi_64 = (int64_t*)va_arg( args, int64_t * );
499 *pi_64 = !p_input->input.p_stream ? 0 :
500 stream_Tell( p_input->input.p_stream );
503 case INPUT_SET_BYTE_SIZE:
504 pi_64 = (int64_t*)va_arg( args, int64_t * );
505 *pi_64 = !p_input->input.p_stream ? 0 :
506 stream_Size( p_input->input.p_stream );
509 case INPUT_ADD_SLAVE:
510 psz = (char*)va_arg( args, char * );
513 val.psz_string = strdup( psz );
514 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
519 msg_Err( p_input, "unknown query in input_vaControl" );
524 static void NotifyPlaylist( input_thread_t *p_input )
526 playlist_t *p_playlist =
527 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
531 var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
532 vlc_object_release( p_playlist );
536 static void UpdateBookmarksOption( input_thread_t *p_input )
539 char *psz_value = NULL, *psz_next = NULL;
541 vlc_mutex_lock( &p_input->input.p_item->lock );
542 for( i = 0; i < p_input->i_bookmark; i++ )
544 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
545 p_input->bookmark[i]->psz_name,
546 p_input->bookmark[i]->i_byte_offset,
547 p_input->bookmark[i]->i_time_offset/1000000 );
548 i_len += strlen( psz_value );
551 for( i = 0; i < p_input->i_bookmark; i++ )
553 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
555 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
556 p_input->bookmark[i]->psz_name,
557 p_input->bookmark[i]->i_byte_offset,
558 p_input->bookmark[i]->i_time_offset/1000000 );
560 psz_next += strlen( psz_next );
561 if( i < p_input->i_bookmark - 1)
563 *psz_next = ','; psz_next++;
566 vlc_mutex_unlock( &p_input->input.p_item->lock );
568 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
569 psz_value ? psz_value : "" );