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;
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 char *psz_cat = (char *)va_arg( args, char * );
133 char *psz_name = (char *)va_arg( args, char * );
134 char *psz_format = (char *)va_arg( args, char * );
136 info_category_t *p_cat;
140 vlc_mutex_lock( &p_input->input.p_item->lock );
141 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
143 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
147 if( i == p_input->input.p_item->i_categories )
149 p_cat = malloc( sizeof( info_category_t ) );
152 vlc_mutex_lock( &p_input->input.p_item->lock );
155 p_cat->psz_name = strdup( psz_cat );
157 p_cat->pp_infos = NULL;
158 INSERT_ELEM( p_input->input.p_item->pp_categories,
159 p_input->input.p_item->i_categories,
160 p_input->input.p_item->i_categories, p_cat );
163 p_cat = p_input->input.p_item->pp_categories[i];
165 for( i = 0; i < p_cat->i_infos; i++ )
167 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
169 if( p_cat->pp_infos[i]->psz_value )
170 free( p_cat->pp_infos[i]->psz_value );
175 if( i == p_cat->i_infos )
177 p_info = malloc( sizeof( info_t ) );
180 vlc_mutex_lock( &p_input->input.p_item->lock );
184 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
185 p_cat->i_infos, p_info );
186 p_info->psz_name = strdup( psz_name );
189 p_info = p_cat->pp_infos[i];
190 vasprintf( &p_info->psz_value, psz_format, args );
192 vlc_mutex_unlock( &p_input->input.p_item->lock );
194 NotifyPlaylist( p_input );
200 char *psz_cat = (char *)va_arg( args, char * );
201 char *psz_name = (char *)va_arg( args, char * );
202 char **ppsz_value = (char **)va_arg( args, char ** );
203 int i_ret = VLC_EGENERIC;
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,
214 if( i != p_input->input.p_item->i_categories )
216 info_category_t *p_cat;
217 p_cat = p_input->input.p_item->pp_categories[i];
219 for( i = 0; i < p_cat->i_infos; i++ )
221 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
223 if( p_cat->pp_infos[i]->psz_value )
225 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
232 vlc_mutex_unlock( &p_input->input.p_item->lock );
238 char *psz_name = (char *)va_arg( args, char * );
240 if( !psz_name ) return VLC_EGENERIC;
242 vlc_mutex_lock( &p_input->input.p_item->lock );
243 if( p_input->input.p_item->psz_name )
244 free( p_input->input.p_item->psz_name );
245 p_input->input.p_item->psz_name = strdup( psz_name );
246 vlc_mutex_unlock( &p_input->input.p_item->lock );
248 NotifyPlaylist( p_input );
253 case INPUT_ADD_BOOKMARK:
254 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
255 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
257 vlc_mutex_lock( &p_input->input.p_item->lock );
258 if( !p_bkmk->psz_name )
260 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
261 p_input->i_bookmark );
264 TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
266 /* Reflect the changes on the object var */
267 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
269 vlc_value_t val, text;
272 for( i = 0; i < p_input->i_bookmark; i++ )
275 text.psz_string = p_input->bookmark[i]->psz_name;
276 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
280 vlc_mutex_unlock( &p_input->input.p_item->lock );
282 UpdateBookmarksOption( p_input );
286 case INPUT_CHANGE_BOOKMARK:
287 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
288 i_bkmk = (int)va_arg( args, int );
290 vlc_mutex_lock( &p_input->input.p_item->lock );
291 if( i_bkmk < p_input->i_bookmark )
293 vlc_value_t val, text;
296 p_input->bookmark[i_bkmk] = p_bkmk;
298 /* Reflect the changes on the object var */
299 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
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_DEL_BOOKMARK:
315 i_bkmk = (int)va_arg( args, int );
317 vlc_mutex_lock( &p_input->input.p_item->lock );
318 if( i_bkmk < p_input->i_bookmark )
320 vlc_value_t val, text;
323 p_bkmk = p_input->bookmark[i_bkmk];
324 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
326 vlc_seekpoint_Delete( p_bkmk );
328 /* Reflect the changes on the object var */
329 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
330 for( i = 0; i < p_input->i_bookmark; i++ )
333 text.psz_string = p_input->bookmark[i]->psz_name;
334 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
337 vlc_mutex_unlock( &p_input->input.p_item->lock );
339 UpdateBookmarksOption( p_input );
343 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 );
400 case INPUT_SET_BOOKMARK:
401 i_bkmk = (int)va_arg( args, int );
403 vlc_mutex_lock( &p_input->input.p_item->lock );
404 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
409 if( p_input->bookmark[i_bkmk]->i_time_offset )
411 pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
412 i_ret = var_Set( p_input, "time", pos );
414 else if( p_input->bookmark[i_bkmk]->i_byte_offset )
416 pos.f_float = !p_input->input.p_stream ? 0 :
417 p_input->bookmark[i_bkmk]->i_byte_offset /
418 stream_Size( p_input->input.p_stream );
419 i_ret = var_Set( p_input, "position", pos );
424 i_ret = var_Set( p_input, "position", pos );
427 vlc_mutex_unlock( &p_input->input.p_item->lock );
432 vlc_mutex_unlock( &p_input->input.p_item->lock );
438 case INPUT_ADD_OPTION:
440 char *psz_option = (char *)va_arg( args, char * );
441 char *psz_value = (char *)va_arg( args, char * );
444 vlc_mutex_lock( &p_input->input.p_item->lock );
445 /* Check if option already exists */
446 for( i = 0; i < p_input->input.p_item->i_options; i++ )
448 if( !strncmp( p_input->input.p_item->ppsz_options[i],
449 psz_option, strlen( psz_option ) ) &&
450 p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
453 free( p_input->input.p_item->ppsz_options[i] );
457 if( i == p_input->input.p_item->i_options )
459 p_input->input.p_item->i_options++;
460 p_input->input.p_item->ppsz_options =
461 realloc( p_input->input.p_item->ppsz_options,
462 p_input->input.p_item->i_options *
466 asprintf( &p_input->input.p_item->ppsz_options[i],
467 "%s=%s", psz_option, psz_value ) ;
468 vlc_mutex_unlock( &p_input->input.p_item->lock );
473 case INPUT_GET_BYTE_POSITION:
474 pi_64 = (int64_t*)va_arg( args, int64_t * );
475 *pi_64 = !p_input->input.p_stream ? 0 :
476 stream_Tell( p_input->input.p_stream );
479 case INPUT_SET_BYTE_SIZE:
480 pi_64 = (int64_t*)va_arg( args, int64_t * );
481 *pi_64 = !p_input->input.p_stream ? 0 :
482 stream_Size( p_input->input.p_stream );
485 case INPUT_ADD_SLAVE:
486 psz = (char*)va_arg( args, char * );
489 val.psz_string = strdup( psz );
490 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
495 msg_Err( p_input, "unknown query in input_vaControl" );
500 static void NotifyPlaylist( input_thread_t *p_input )
502 playlist_t *p_playlist =
503 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
507 var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
508 vlc_object_release( p_playlist );
512 static void UpdateBookmarksOption( input_thread_t *p_input )
515 char *psz_value = NULL, *psz_next = NULL;
517 vlc_mutex_lock( &p_input->input.p_item->lock );
518 for( i = 0; i < p_input->i_bookmark; i++ )
520 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
521 p_input->bookmark[i]->psz_name,
522 p_input->bookmark[i]->i_byte_offset,
523 p_input->bookmark[i]->i_time_offset/1000000 );
524 i_len += strlen( psz_value );
527 for( i = 0; i < p_input->i_bookmark; i++ )
529 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
531 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
532 p_input->bookmark[i]->psz_name,
533 p_input->bookmark[i]->i_byte_offset,
534 p_input->bookmark[i]->i_time_offset/1000000 );
536 psz_next += strlen( psz_next );
537 if( i < p_input->i_bookmark - 1)
539 *psz_next = ','; psz_next++;
542 vlc_mutex_unlock( &p_input->input.p_item->lock );
544 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
545 psz_value ? psz_value : "" );