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 *****************************************************************************/
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;
68 case INPUT_GET_POSITION:
69 pf = (double*)va_arg( args, double * );
70 *pf = var_GetFloat( p_input, "position" );
73 case INPUT_SET_POSITION:
74 f = (double)va_arg( args, double );
75 return var_SetFloat( p_input, "position", f );
77 case INPUT_GET_LENGTH:
78 pi_64 = (int64_t*)va_arg( args, int64_t * );
79 *pi_64 = var_GetTime( p_input, "length" );
83 pi_64 = (int64_t*)va_arg( args, int64_t * );
84 *pi_64 = var_GetTime( p_input, "time" );
88 i_64 = (int64_t)va_arg( args, int64_t );
89 return var_SetTime( p_input, "time", i_64 );
92 pi_int = (int*)va_arg( args, int * );
93 *pi_int = var_GetInteger( p_input, "rate" );
97 i_int = (int)va_arg( args, int );
98 return var_SetInteger( p_input, "rate", i_int );
100 case INPUT_GET_STATE:
101 pi_int = (int*)va_arg( args, int * );
102 *pi_int = var_GetInteger( p_input, "state" );
105 case INPUT_SET_STATE:
106 i_int = (int)va_arg( args, int );
107 return var_SetInteger( p_input, "state", i_int );
109 case INPUT_GET_AUDIO_DELAY:
110 pi_64 = (int64_t*)va_arg( args, int64_t * );
111 *pi_64 = var_GetTime( p_input, "audio-delay" );
114 case INPUT_GET_SPU_DELAY:
115 pi_64 = (int64_t*)va_arg( args, int64_t * );
116 *pi_64 = var_GetTime( p_input, "spu-delay" );
119 case INPUT_SET_AUDIO_DELAY:
120 i_64 = (int64_t)va_arg( args, int64_t );
121 return var_SetTime( p_input, "audio-delay", i_64 );
123 case INPUT_SET_SPU_DELAY:
124 i_64 = (int64_t)va_arg( args, int64_t );
125 return var_SetTime( p_input, "spu-delay", i_64 );
129 char *psz_cat = (char *)va_arg( args, char * );
130 char *psz_name = (char *)va_arg( args, char * );
131 char *psz_format = (char *)va_arg( args, char * );
133 info_category_t *p_cat;
137 vlc_mutex_lock( &p_input->input.p_item->lock );
138 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
140 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
144 if( i == p_input->input.p_item->i_categories )
146 p_cat = malloc( sizeof( info_category_t ) );
149 vlc_mutex_lock( &p_input->input.p_item->lock );
152 p_cat->psz_name = strdup( psz_cat );
154 p_cat->pp_infos = NULL;
155 INSERT_ELEM( p_input->input.p_item->pp_categories,
156 p_input->input.p_item->i_categories,
157 p_input->input.p_item->i_categories, p_cat );
160 p_cat = p_input->input.p_item->pp_categories[i];
162 for( i = 0; i < p_cat->i_infos; i++ )
164 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
166 if( p_cat->pp_infos[i]->psz_value )
167 free( p_cat->pp_infos[i]->psz_value );
172 if( i == p_cat->i_infos )
174 p_info = malloc( sizeof( info_t ) );
177 vlc_mutex_lock( &p_input->input.p_item->lock );
181 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
182 p_cat->i_infos, p_info );
183 p_info->psz_name = strdup( psz_name );
186 p_info = p_cat->pp_infos[i];
187 vasprintf( &p_info->psz_value, psz_format, args );
189 vlc_mutex_unlock( &p_input->input.p_item->lock );
191 NotifyPlaylist( p_input );
197 char *psz_cat = (char *)va_arg( args, char * );
198 char *psz_name = (char *)va_arg( args, char * );
199 char **ppsz_value = (char **)va_arg( args, char ** );
200 int i_ret = VLC_EGENERIC;
204 vlc_mutex_lock( &p_input->input.p_item->lock );
205 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
207 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
211 if( i != p_input->input.p_item->i_categories )
213 info_category_t *p_cat;
214 p_cat = p_input->input.p_item->pp_categories[i];
216 for( i = 0; i < p_cat->i_infos; i++ )
218 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
220 if( p_cat->pp_infos[i]->psz_value )
222 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
229 vlc_mutex_unlock( &p_input->input.p_item->lock );
235 char *psz_name = (char *)va_arg( args, char * );
237 if( !psz_name ) return VLC_EGENERIC;
239 vlc_mutex_lock( &p_input->input.p_item->lock );
240 if( p_input->input.p_item->psz_name )
241 free( p_input->input.p_item->psz_name );
242 p_input->input.p_item->psz_name = strdup( psz_name );
243 vlc_mutex_unlock( &p_input->input.p_item->lock );
245 NotifyPlaylist( p_input );
250 case INPUT_ADD_BOOKMARK:
251 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
252 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
254 vlc_mutex_lock( &p_input->input.p_item->lock );
255 if( !p_bkmk->psz_name )
257 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
258 p_input->i_bookmark );
261 TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
263 /* Reflect the changes on the object var */
264 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
266 vlc_value_t val, text;
269 for( i = 0; i < p_input->i_bookmark; i++ )
272 text.psz_string = p_input->bookmark[i]->psz_name;
273 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
277 vlc_mutex_unlock( &p_input->input.p_item->lock );
279 UpdateBookmarksOption( p_input );
284 case INPUT_CHANGE_BOOKMARK:
285 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
286 i_bkmk = (int)va_arg( args, int );
288 vlc_mutex_lock( &p_input->input.p_item->lock );
289 if( i_bkmk < p_input->i_bookmark )
291 vlc_value_t val, text;
294 p_input->bookmark[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_bookmark; i++ )
301 text.psz_string = p_input->bookmark[i]->psz_name;
302 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
306 vlc_mutex_unlock( &p_input->input.p_item->lock );
308 UpdateBookmarksOption( p_input );
313 case INPUT_DEL_BOOKMARK:
314 i_bkmk = (int)va_arg( args, int );
316 vlc_mutex_lock( &p_input->input.p_item->lock );
317 if( i_bkmk < p_input->i_bookmark )
319 vlc_value_t val, text;
322 p_bkmk = p_input->bookmark[i_bkmk];
323 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
325 vlc_seekpoint_Delete( p_bkmk );
327 /* Reflect the changes on the object var */
328 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
329 for( i = 0; i < p_input->i_bookmark; i++ )
332 text.psz_string = p_input->bookmark[i]->psz_name;
333 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
336 vlc_mutex_unlock( &p_input->input.p_item->lock );
338 UpdateBookmarksOption( p_input );
342 vlc_mutex_unlock( &p_input->input.p_item->lock );
347 case INPUT_GET_BOOKMARKS:
348 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
349 pi_bkmk = (int *)va_arg( args, int * );
351 vlc_mutex_lock( &p_input->input.p_item->lock );
352 if( p_input->i_bookmark )
356 *pi_bkmk = p_input->i_bookmark;
357 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
358 p_input->i_bookmark );
359 for( i = 0; i < p_input->i_bookmark; i++ )
362 vlc_seekpoint_Duplicate(p_input->bookmark[i]);
365 vlc_mutex_unlock( &p_input->input.p_item->lock );
373 vlc_mutex_unlock( &p_input->input.p_item->lock );
378 case INPUT_CLEAR_BOOKMARKS:
380 vlc_mutex_lock( &p_input->input.p_item->lock );
381 if( p_input->i_bookmark )
385 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
387 p_bkmk = p_input->bookmark[i];
388 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
390 vlc_seekpoint_Delete( p_bkmk );
392 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
394 vlc_mutex_unlock( &p_input->input.p_item->lock );
396 UpdateBookmarksOption( p_input );
401 case INPUT_SET_BOOKMARK:
402 i_bkmk = (int)va_arg( args, int );
404 vlc_mutex_lock( &p_input->input.p_item->lock );
405 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
410 if( p_input->bookmark[i_bkmk]->i_time_offset )
412 pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
413 i_ret = var_Set( p_input, "time", pos );
415 else if( p_input->bookmark[i_bkmk]->i_byte_offset )
417 pos.f_float = !p_input->input.p_stream ? 0 :
418 p_input->bookmark[i_bkmk]->i_byte_offset /
419 stream_Size( p_input->input.p_stream );
420 i_ret = var_Set( p_input, "position", pos );
425 i_ret = var_Set( p_input, "position", pos );
428 vlc_mutex_unlock( &p_input->input.p_item->lock );
433 vlc_mutex_unlock( &p_input->input.p_item->lock );
439 case INPUT_ADD_OPTION:
441 char *psz_option = (char *)va_arg( args, char * );
442 char *psz_value = (char *)va_arg( args, char * );
445 vlc_mutex_lock( &p_input->input.p_item->lock );
446 /* Check if option already exists */
447 for( i = 0; i < p_input->input.p_item->i_options; i++ )
449 if( !strncmp( p_input->input.p_item->ppsz_options[i],
450 psz_option, strlen( psz_option ) ) &&
451 p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
454 free( p_input->input.p_item->ppsz_options[i] );
458 if( i == p_input->input.p_item->i_options )
460 p_input->input.p_item->i_options++;
461 p_input->input.p_item->ppsz_options =
462 realloc( p_input->input.p_item->ppsz_options,
463 p_input->input.p_item->i_options *
467 asprintf( &p_input->input.p_item->ppsz_options[i],
468 "%s=%s", psz_option, psz_value ) ;
469 vlc_mutex_unlock( &p_input->input.p_item->lock );
475 case INPUT_GET_BYTE_POSITION:
476 pi_64 = (int64_t*)va_arg( args, int64_t * );
477 *pi_64 = !p_input->input.p_stream ? 0 :
478 stream_Tell( p_input->input.p_stream );
482 case INPUT_SET_BYTE_SIZE:
483 pi_64 = (int64_t*)va_arg( args, int64_t * );
484 *pi_64 = !p_input->input.p_stream ? 0 :
485 stream_Size( p_input->input.p_stream );
490 msg_Err( p_input, "unknown query in input_vaControl" );
495 static void NotifyPlaylist( input_thread_t *p_input )
497 playlist_t *p_playlist =
498 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
502 var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
503 vlc_object_release( p_playlist );
507 static void UpdateBookmarksOption( input_thread_t *p_input )
510 char *psz_value = NULL, *psz_next = NULL;
512 vlc_mutex_lock( &p_input->input.p_item->lock );
513 for( i = 0; i < p_input->i_bookmark; i++ )
515 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
516 p_input->bookmark[i]->psz_name,
517 p_input->bookmark[i]->i_byte_offset,
518 p_input->bookmark[i]->i_time_offset/1000000 );
519 i_len += strlen( psz_value );
522 for( i = 0; i < p_input->i_bookmark; i++ )
524 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
526 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
527 p_input->bookmark[i]->psz_name,
528 p_input->bookmark[i]->i_byte_offset,
529 p_input->bookmark[i]->i_time_offset/1000000 );
531 psz_next += strlen( psz_next );
532 if( i < p_input->i_bookmark - 1)
534 *psz_next = ','; psz_next++;
537 vlc_mutex_unlock( &p_input->input.p_item->lock );
539 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
540 psz_value ? psz_value : "" );