1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc/input.h>
30 #include "input_internal.h"
31 #include "vlc_playlist.h"
34 static void UpdateBookmarksOption( input_thread_t * );
35 static void NotifyPlaylist( input_thread_t * );
37 /****************************************************************************
39 ****************************************************************************/
41 * Control function for inputs.
42 * \param p_input input handle
43 * \param i_query query type
44 * \return VLC_SUCCESS if ok
46 int input_Control( input_thread_t *p_input, int i_query, ... )
51 va_start( args, i_query );
52 i_result = input_vaControl( p_input, i_query, args );
58 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
60 seekpoint_t *p_bkmk, ***ppp_bkmk;
73 case INPUT_GET_POSITION:
74 pf = (double*)va_arg( args, double * );
75 *pf = var_GetFloat( p_input, "position" );
78 case INPUT_SET_POSITION:
79 f = (double)va_arg( args, double );
80 return var_SetFloat( p_input, "position", f );
82 case INPUT_GET_LENGTH:
83 pi_64 = (int64_t*)va_arg( args, int64_t * );
84 *pi_64 = var_GetTime( p_input, "length" );
88 pi_64 = (int64_t*)va_arg( args, int64_t * );
89 *pi_64 = var_GetTime( p_input, "time" );
93 i_64 = (int64_t)va_arg( args, int64_t );
94 return var_SetTime( p_input, "time", i_64 );
97 pi_int = (int*)va_arg( args, int * );
98 *pi_int = var_GetInteger( p_input, "rate" );
102 i_int = (int)va_arg( args, int );
103 return var_SetInteger( p_input, "rate", i_int );
105 case INPUT_GET_STATE:
106 pi_int = (int*)va_arg( args, int * );
107 *pi_int = var_GetInteger( p_input, "state" );
110 case INPUT_SET_STATE:
111 i_int = (int)va_arg( args, int );
112 return var_SetInteger( p_input, "state", i_int );
114 case INPUT_GET_AUDIO_DELAY:
115 pi_64 = (int64_t*)va_arg( args, int64_t * );
116 *pi_64 = var_GetTime( p_input, "audio-delay" );
119 case INPUT_GET_SPU_DELAY:
120 pi_64 = (int64_t*)va_arg( args, int64_t * );
121 *pi_64 = var_GetTime( p_input, "spu-delay" );
124 case INPUT_SET_AUDIO_DELAY:
125 i_64 = (int64_t)va_arg( args, int64_t );
126 return var_SetTime( p_input, "audio-delay", i_64 );
128 case INPUT_SET_SPU_DELAY:
129 i_64 = (int64_t)va_arg( args, int64_t );
130 return var_SetTime( p_input, "spu-delay", i_64 );
134 /* FIXME : Impossible to use input_ItemAddInfo because of
135 * the ... problem ? */
136 char *psz_cat = (char *)va_arg( args, char * );
137 char *psz_name = (char *)va_arg( args, char * );
138 char *psz_format = (char *)va_arg( args, char * );
140 info_category_t *p_cat;
144 vlc_mutex_lock( &p_input->input.p_item->lock );
145 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
147 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
151 if( i == p_input->input.p_item->i_categories )
153 p_cat = malloc( sizeof( info_category_t ) );
156 vlc_mutex_lock( &p_input->input.p_item->lock );
159 p_cat->psz_name = strdup( psz_cat );
161 p_cat->pp_infos = NULL;
162 INSERT_ELEM( p_input->input.p_item->pp_categories,
163 p_input->input.p_item->i_categories,
164 p_input->input.p_item->i_categories, p_cat );
167 p_cat = p_input->input.p_item->pp_categories[i];
169 for( i = 0; i < p_cat->i_infos; i++ )
171 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
173 if( p_cat->pp_infos[i]->psz_value )
174 free( p_cat->pp_infos[i]->psz_value );
179 if( i == p_cat->i_infos )
181 p_info = malloc( sizeof( info_t ) );
184 vlc_mutex_lock( &p_input->input.p_item->lock );
188 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
189 p_cat->i_infos, p_info );
190 p_info->psz_name = strdup( psz_name );
193 p_info = p_cat->pp_infos[i];
194 vasprintf( &p_info->psz_value, psz_format, args );
196 vlc_mutex_unlock( &p_input->input.p_item->lock );
198 if( !p_input->b_preparsing )
199 NotifyPlaylist( p_input );
205 char *psz_cat = (char *)va_arg( args, char * );
206 char *psz_name = (char *)va_arg( args, char * );
208 info_category_t *p_cat = NULL;
211 vlc_mutex_lock( &p_input->input.p_item->lock );
212 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
214 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
217 p_cat = p_input->input.p_item->pp_categories[i];
223 vlc_mutex_unlock( &p_input->input.p_item->lock );
227 for( i = 0; i < p_cat->i_infos; i++ )
229 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
231 free( p_cat->pp_infos[i]->psz_name );
232 if( p_cat->pp_infos[i]->psz_value )
233 free( p_cat->pp_infos[i]->psz_value );
234 free( p_cat->pp_infos[i] );
235 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
239 vlc_mutex_unlock( &p_input->input.p_item->lock );
241 if( i >= p_cat->i_infos )
244 if( !p_input->b_preparsing )
245 NotifyPlaylist( p_input );
252 char *psz_cat = (char *)va_arg( args, char * );
253 char *psz_name = (char *)va_arg( args, char * );
254 char **ppsz_value = (char **)va_arg( args, char ** );
255 int i_ret = VLC_EGENERIC;
258 *ppsz_value = input_ItemGetInfo( p_input->input.p_item,
265 char *psz_name = (char *)va_arg( args, char * );
267 if( !psz_name ) return VLC_EGENERIC;
269 vlc_mutex_lock( &p_input->input.p_item->lock );
270 if( p_input->input.p_item->psz_name )
271 free( p_input->input.p_item->psz_name );
272 p_input->input.p_item->psz_name = strdup( psz_name );
273 vlc_mutex_unlock( &p_input->input.p_item->lock );
275 if( !p_input->b_preparsing )
276 NotifyPlaylist( p_input );
281 case INPUT_ADD_BOOKMARK:
282 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
283 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
285 vlc_mutex_lock( &p_input->input.p_item->lock );
286 if( !p_bkmk->psz_name )
288 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
289 p_input->i_bookmark );
292 TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
294 /* Reflect the changes on the object var */
295 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
297 vlc_value_t val, text;
300 for( i = 0; i < p_input->i_bookmark; i++ )
303 text.psz_string = p_input->bookmark[i]->psz_name;
304 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
308 vlc_mutex_unlock( &p_input->input.p_item->lock );
310 UpdateBookmarksOption( p_input );
314 case INPUT_CHANGE_BOOKMARK:
315 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
316 i_bkmk = (int)va_arg( args, int );
318 vlc_mutex_lock( &p_input->input.p_item->lock );
319 if( i_bkmk < p_input->i_bookmark )
321 vlc_value_t val, text;
324 p_input->bookmark[i_bkmk] = p_bkmk;
326 /* Reflect the changes on the object var */
327 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
328 for( i = 0; i < p_input->i_bookmark; i++ )
331 text.psz_string = p_input->bookmark[i]->psz_name;
332 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
336 vlc_mutex_unlock( &p_input->input.p_item->lock );
338 UpdateBookmarksOption( p_input );
342 case INPUT_DEL_BOOKMARK:
343 i_bkmk = (int)va_arg( args, int );
345 vlc_mutex_lock( &p_input->input.p_item->lock );
346 if( i_bkmk < p_input->i_bookmark )
348 vlc_value_t val, text;
351 p_bkmk = p_input->bookmark[i_bkmk];
352 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
354 vlc_seekpoint_Delete( p_bkmk );
356 /* Reflect the changes on the object var */
357 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
358 for( i = 0; i < p_input->i_bookmark; i++ )
361 text.psz_string = p_input->bookmark[i]->psz_name;
362 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
365 vlc_mutex_unlock( &p_input->input.p_item->lock );
367 UpdateBookmarksOption( p_input );
371 vlc_mutex_unlock( &p_input->input.p_item->lock );
375 case INPUT_GET_BOOKMARKS:
376 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
377 pi_bkmk = (int *)va_arg( args, int * );
379 vlc_mutex_lock( &p_input->input.p_item->lock );
380 if( p_input->i_bookmark )
384 *pi_bkmk = p_input->i_bookmark;
385 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
386 p_input->i_bookmark );
387 for( i = 0; i < p_input->i_bookmark; i++ )
390 vlc_seekpoint_Duplicate(p_input->bookmark[i]);
393 vlc_mutex_unlock( &p_input->input.p_item->lock );
401 vlc_mutex_unlock( &p_input->input.p_item->lock );
406 case INPUT_CLEAR_BOOKMARKS:
408 vlc_mutex_lock( &p_input->input.p_item->lock );
409 if( p_input->i_bookmark )
413 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
415 p_bkmk = p_input->bookmark[i];
416 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
418 vlc_seekpoint_Delete( p_bkmk );
420 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
422 vlc_mutex_unlock( &p_input->input.p_item->lock );
424 UpdateBookmarksOption( p_input );
428 case INPUT_SET_BOOKMARK:
429 i_bkmk = (int)va_arg( args, int );
431 vlc_mutex_lock( &p_input->input.p_item->lock );
432 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
437 if( p_input->bookmark[i_bkmk]->i_time_offset != -1 )
439 pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
440 i_ret = var_Set( p_input, "time", pos );
442 else if( p_input->bookmark[i_bkmk]->i_byte_offset != -1 )
444 // don't crash on bookmarks in live streams
445 if( stream_Size( p_input->input.p_stream ) == 0 )
447 vlc_mutex_unlock( &p_input->input.p_item->lock );
450 pos.f_float = !p_input->input.p_stream ? 0 :
451 p_input->bookmark[i_bkmk]->i_byte_offset /
452 stream_Size( p_input->input.p_stream );
453 i_ret = var_Set( p_input, "position", pos );
458 i_ret = var_Set( p_input, "position", pos );
461 vlc_mutex_unlock( &p_input->input.p_item->lock );
466 vlc_mutex_unlock( &p_input->input.p_item->lock );
472 case INPUT_ADD_OPTION:
474 char *psz_option = (char *)va_arg( args, char * );
475 char *psz_value = (char *)va_arg( args, char * );
478 vlc_mutex_lock( &p_input->input.p_item->lock );
479 /* Check if option already exists */
480 for( i = 0; i < p_input->input.p_item->i_options; i++ )
482 if( !strncmp( p_input->input.p_item->ppsz_options[i],
483 psz_option, strlen( psz_option ) ) &&
484 p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
487 free( p_input->input.p_item->ppsz_options[i] );
491 if( i == p_input->input.p_item->i_options )
493 p_input->input.p_item->i_options++;
494 p_input->input.p_item->ppsz_options =
495 realloc( p_input->input.p_item->ppsz_options,
496 p_input->input.p_item->i_options *
500 asprintf( &p_input->input.p_item->ppsz_options[i],
501 "%s=%s", psz_option, psz_value ) ;
502 vlc_mutex_unlock( &p_input->input.p_item->lock );
507 case INPUT_GET_BYTE_POSITION:
508 pi_64 = (int64_t*)va_arg( args, int64_t * );
509 *pi_64 = !p_input->input.p_stream ? 0 :
510 stream_Tell( p_input->input.p_stream );
513 case INPUT_SET_BYTE_SIZE:
514 pi_64 = (int64_t*)va_arg( args, int64_t * );
515 *pi_64 = !p_input->input.p_stream ? 0 :
516 stream_Size( p_input->input.p_stream );
519 case INPUT_ADD_SLAVE:
520 psz = (char*)va_arg( args, char * );
523 val.psz_string = strdup( psz );
524 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
529 msg_Err( p_input, "unknown query in input_vaControl" );
534 static void NotifyPlaylist( input_thread_t *p_input )
536 playlist_t *p_playlist = pl_Yield( p_input );
537 var_SetInteger( p_playlist, "item-change",
538 p_input->input.p_item->i_id );
539 pl_Release( p_input );
542 static void UpdateBookmarksOption( input_thread_t *p_input )
545 char *psz_value = NULL, *psz_next = NULL;
547 vlc_mutex_lock( &p_input->input.p_item->lock );
548 for( i = 0; i < p_input->i_bookmark; i++ )
550 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
551 p_input->bookmark[i]->psz_name,
552 p_input->bookmark[i]->i_byte_offset,
553 p_input->bookmark[i]->i_time_offset/1000000 );
554 i_len += strlen( psz_value );
557 for( i = 0; i < p_input->i_bookmark; i++ )
559 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
561 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
562 p_input->bookmark[i]->psz_name,
563 p_input->bookmark[i]->i_byte_offset,
564 p_input->bookmark[i]->i_time_offset/1000000 );
566 psz_next += strlen( psz_next );
567 if( i < p_input->i_bookmark - 1)
569 *psz_next = ','; psz_next++;
572 vlc_mutex_unlock( &p_input->input.p_item->lock );
574 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
575 psz_value ? psz_value : "" );