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_common.h>
33 #include "input_internal.h"
38 static void UpdateBookmarksOption( input_thread_t * );
40 /****************************************************************************
42 ****************************************************************************/
44 * Control function for inputs.
45 * \param p_input input handle
46 * \param i_query query type
47 * \return VLC_SUCCESS if ok
49 int input_Control( input_thread_t *p_input, int i_query, ... )
54 va_start( args, i_query );
55 i_result = input_vaControl( p_input, i_query, args );
61 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
63 seekpoint_t *p_bkmk, ***ppp_bkmk;
68 bool b_bool, *pb_bool;
77 case INPUT_GET_POSITION:
78 pf = (double*)va_arg( args, double * );
79 *pf = var_GetFloat( p_input, "position" );
82 case INPUT_SET_POSITION:
83 f = (double)va_arg( args, double );
84 return var_SetFloat( p_input, "position", f );
86 case INPUT_GET_LENGTH:
87 pi_64 = (int64_t*)va_arg( args, int64_t * );
88 *pi_64 = var_GetTime( p_input, "length" );
92 pi_64 = (int64_t*)va_arg( args, int64_t * );
93 *pi_64 = var_GetTime( p_input, "time" );
97 i_64 = (int64_t)va_arg( args, int64_t );
98 return var_SetTime( p_input, "time", i_64 );
101 pi_int = (int*)va_arg( args, int * );
102 *pi_int = var_GetInteger( p_input, "rate" );
106 i_int = (int)va_arg( args, int );
107 return var_SetInteger( p_input, "rate", i_int );
109 case INPUT_GET_STATE:
110 pi_int = (int*)va_arg( args, int * );
111 *pi_int = var_GetInteger( p_input, "state" );
114 case INPUT_SET_STATE:
115 i_int = (int)va_arg( args, int );
116 return var_SetInteger( p_input, "state", i_int );
118 case INPUT_GET_AUDIO_DELAY:
119 pi_64 = (int64_t*)va_arg( args, int64_t * );
120 *pi_64 = var_GetTime( p_input, "audio-delay" );
123 case INPUT_GET_SPU_DELAY:
124 pi_64 = (int64_t*)va_arg( args, int64_t * );
125 *pi_64 = var_GetTime( p_input, "spu-delay" );
128 case INPUT_SET_AUDIO_DELAY:
129 i_64 = (int64_t)va_arg( args, int64_t );
130 return var_SetTime( p_input, "audio-delay", i_64 );
132 case INPUT_SET_SPU_DELAY:
133 i_64 = (int64_t)va_arg( args, int64_t );
134 return var_SetTime( p_input, "spu-delay", i_64 );
138 char *psz_cat = (char *)va_arg( args, char * );
139 char *psz_name = (char *)va_arg( args, char * );
140 char *psz_format = (char *)va_arg( args, char * );
144 if( vasprintf( &psz_value, psz_format, args ) == -1 )
147 int i_ret = input_item_AddInfo( p_input->p->p_item,
148 psz_cat, psz_name, "%s", psz_value );
151 if( !p_input->b_preparsing && !i_ret )
152 input_SendEventMetaInfo( p_input );
157 char *psz_cat = (char *)va_arg( args, char * );
158 char *psz_name = (char *)va_arg( args, char * );
160 int i_ret = input_item_DelInfo( p_input->p->p_item,
163 if( !p_input->b_preparsing && !i_ret )
164 input_SendEventMetaInfo( p_input );
169 char *psz_cat = (char *)va_arg( args, char * );
170 char *psz_name = (char *)va_arg( args, char * );
171 char **ppsz_value = (char **)va_arg( args, char ** );
172 int i_ret = VLC_EGENERIC;
175 *ppsz_value = input_item_GetInfo( p_input->p->p_item,
182 char *psz_name = (char *)va_arg( args, char * );
184 if( !psz_name ) return VLC_EGENERIC;
186 input_item_SetName( p_input->p->p_item, psz_name );
188 if( !p_input->b_preparsing )
189 input_SendEventMetaName( p_input, psz_name );
193 case INPUT_ADD_BOOKMARK:
194 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
195 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
197 vlc_mutex_lock( &p_input->p->p_item->lock );
198 if( !p_bkmk->psz_name )
200 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
201 p_input->p->i_bookmark ) == -1 )
202 p_bkmk->psz_name = NULL;
205 TAB_APPEND( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
206 vlc_mutex_unlock( &p_input->p->p_item->lock );
208 UpdateBookmarksOption( p_input );
212 case INPUT_CHANGE_BOOKMARK:
213 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
214 i_bkmk = (int)va_arg( args, int );
216 vlc_mutex_lock( &p_input->p->p_item->lock );
217 if( i_bkmk < p_input->p->i_bookmark )
219 vlc_seekpoint_Delete( p_input->p->pp_bookmark[i_bkmk] );
220 p_input->p->pp_bookmark[i_bkmk] = vlc_seekpoint_Duplicate( p_bkmk );
222 vlc_mutex_unlock( &p_input->p->p_item->lock );
224 UpdateBookmarksOption( p_input );
228 case INPUT_DEL_BOOKMARK:
229 i_bkmk = (int)va_arg( args, int );
231 vlc_mutex_lock( &p_input->p->p_item->lock );
232 if( i_bkmk < p_input->p->i_bookmark )
234 p_bkmk = p_input->p->pp_bookmark[i_bkmk];
235 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
236 vlc_seekpoint_Delete( p_bkmk );
238 vlc_mutex_unlock( &p_input->p->p_item->lock );
240 UpdateBookmarksOption( p_input );
244 vlc_mutex_unlock( &p_input->p->p_item->lock );
248 case INPUT_GET_BOOKMARKS:
249 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
250 pi_bkmk = (int *)va_arg( args, int * );
252 vlc_mutex_lock( &p_input->p->p_item->lock );
253 if( p_input->p->i_bookmark )
257 *pi_bkmk = p_input->p->i_bookmark;
258 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
259 p_input->p->i_bookmark );
260 for( i = 0; i < p_input->p->i_bookmark; i++ )
263 vlc_seekpoint_Duplicate( p_input->p->pp_bookmark[i] );
266 vlc_mutex_unlock( &p_input->p->p_item->lock );
274 vlc_mutex_unlock( &p_input->p->p_item->lock );
279 case INPUT_CLEAR_BOOKMARKS:
281 vlc_mutex_lock( &p_input->p->p_item->lock );
282 while( p_input->p->i_bookmark > 0 )
284 p_bkmk = p_input->p->pp_bookmark[p_input->p->i_bookmark-1];
286 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark,
288 vlc_seekpoint_Delete( p_bkmk );
290 vlc_mutex_unlock( &p_input->p->p_item->lock );
292 UpdateBookmarksOption( p_input );
295 case INPUT_SET_BOOKMARK:
296 i_bkmk = (int)va_arg( args, int );
299 input_ControlPush( p_input, INPUT_CONTROL_SET_BOOKMARK, &val );
303 case INPUT_GET_BOOKMARK:
304 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
306 vlc_mutex_lock( &p_input->p->p_item->lock );
307 *p_bkmk = p_input->p->bookmark;
308 vlc_mutex_unlock( &p_input->p->p_item->lock );
311 case INPUT_ADD_OPTION:
313 const char *psz_option = va_arg( args, const char * );
314 const char *psz_value = va_arg( args, const char * );
318 if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
321 i = input_item_AddOption( p_input->p->p_item, str,
322 VLC_INPUT_OPTION_UNIQUE );
327 case INPUT_GET_VIDEO_FPS:
328 pf = (double*)va_arg( args, double * );
330 vlc_mutex_lock( &p_input->p->p_item->lock );
331 *pf = p_input->p->f_fps;
332 vlc_mutex_unlock( &p_input->p->p_item->lock );
335 case INPUT_ADD_SLAVE:
336 psz = (char*)va_arg( args, char * );
339 val.psz_string = strdup( psz );
340 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
344 case INPUT_ADD_SUBTITLE:
345 psz = (char*)va_arg( args, char * );
346 b_bool = (bool)va_arg( args, int );
348 if( !psz || *psz == '\0' )
350 if( b_bool && !subtitles_Filter( psz ) )
353 val.psz_string = strdup( psz );
354 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
357 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
359 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
360 int *pi_attachment = (int*)va_arg( args, int * );
363 vlc_mutex_lock( &p_input->p->p_item->lock );
364 if( p_input->p->i_attachment <= 0 )
366 vlc_mutex_unlock( &p_input->p->p_item->lock );
367 *ppp_attachment = NULL;
371 *pi_attachment = p_input->p->i_attachment;
372 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
373 for( i = 0; i < p_input->p->i_attachment; i++ )
374 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
376 vlc_mutex_unlock( &p_input->p->p_item->lock );
380 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
382 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
383 const char *psz_name = (const char*)va_arg( args, const char * );
386 vlc_mutex_lock( &p_input->p->p_item->lock );
387 for( i = 0; i < p_input->p->i_attachment; i++ )
389 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
391 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
392 vlc_mutex_unlock( &p_input->p->p_item->lock );
396 *pp_attachment = NULL;
397 vlc_mutex_unlock( &p_input->p->p_item->lock );
401 case INPUT_SET_RECORD_STATE:
402 b_bool = (bool)va_arg( args, int );
403 var_SetBool( p_input, "record", b_bool );
406 case INPUT_GET_RECORD_STATE:
407 pb_bool = (bool*)va_arg( args, bool* );
408 *pb_bool = var_GetBool( p_input, "record" );
411 case INPUT_RESTART_ES:
412 val.i_int = (int)va_arg( args, int );
413 input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
418 aout_instance_t *p_aout = input_resource_HoldAout( p_input->p->p_resource );
422 aout_instance_t **pp_aout = (aout_instance_t**)va_arg( args, aout_instance_t** );
427 case INPUT_GET_VOUTS:
429 vout_thread_t ***ppp_vout = (vout_thread_t***)va_arg( args, vout_thread_t*** );
430 int *pi_vout = (int*)va_arg( args, int* );
432 input_resource_HoldVouts( p_input->p->p_resource, ppp_vout, pi_vout );
439 msg_Err( p_input, "unknown query in input_vaControl" );
444 static void UpdateBookmarksOption( input_thread_t *p_input )
446 vlc_mutex_lock( &p_input->p->p_item->lock );
448 /* Update the "bookmark" list */
449 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
450 for( int i = 0; i < p_input->p->i_bookmark; i++ )
452 vlc_value_t val, text;
455 text.psz_string = p_input->p->pp_bookmark[i]->psz_name;
456 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
460 /* Create the "bookmarks" option value */
461 const char *psz_format = "{name=%s,bytes=%"PRId64",time=%"PRId64"}";
462 int i_len = strlen( "bookmarks=" );
463 for( int i = 0; i < p_input->p->i_bookmark; i++ )
465 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
467 i_len += snprintf( NULL, 0, psz_format,
468 p_bookmark->psz_name,
469 p_bookmark->i_byte_offset,
470 p_bookmark->i_time_offset/1000000 );
473 char *psz_value = malloc( i_len + p_input->p->i_bookmark + 1 );
474 char *psz_next = psz_value;
476 psz_next += sprintf( psz_next, "bookmarks=" );
477 for( int i = 0; i < p_input->p->i_bookmark && psz_value != NULL; i++ )
479 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
481 psz_next += sprintf( psz_next, psz_format,
482 p_bookmark->psz_name,
483 p_bookmark->i_byte_offset,
484 p_bookmark->i_time_offset/1000000 );
486 if( i < p_input->p->i_bookmark - 1)
489 vlc_mutex_unlock( &p_input->p->p_item->lock );
492 input_item_AddOption( p_input->p->p_item, psz_value, VLC_INPUT_OPTION_UNIQUE );
496 input_SendEventBookmark( p_input );