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"
37 static void UpdateBookmarksOption( input_thread_t * );
39 /****************************************************************************
41 ****************************************************************************/
43 * Control function for inputs.
44 * \param p_input input handle
45 * \param i_query query type
46 * \return VLC_SUCCESS if ok
48 int input_Control( input_thread_t *p_input, int i_query, ... )
53 va_start( args, i_query );
54 i_result = input_vaControl( p_input, i_query, args );
60 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
62 seekpoint_t *p_bkmk, ***ppp_bkmk;
67 bool b_bool, *pb_bool;
76 case INPUT_GET_POSITION:
77 pf = (double*)va_arg( args, double * );
78 *pf = var_GetFloat( p_input, "position" );
81 case INPUT_SET_POSITION:
82 f = (double)va_arg( args, double );
83 return var_SetFloat( p_input, "position", f );
85 case INPUT_GET_LENGTH:
86 pi_64 = (int64_t*)va_arg( args, int64_t * );
87 *pi_64 = var_GetTime( p_input, "length" );
91 pi_64 = (int64_t*)va_arg( args, int64_t * );
92 *pi_64 = var_GetTime( p_input, "time" );
96 i_64 = (int64_t)va_arg( args, int64_t );
97 return var_SetTime( p_input, "time", i_64 );
100 pi_int = (int*)va_arg( args, int * );
101 *pi_int = var_GetInteger( p_input, "rate" );
105 i_int = (int)va_arg( args, int );
106 return var_SetInteger( p_input, "rate", i_int );
108 case INPUT_GET_STATE:
109 pi_int = (int*)va_arg( args, int * );
110 *pi_int = var_GetInteger( p_input, "state" );
113 case INPUT_SET_STATE:
114 i_int = (int)va_arg( args, int );
115 return var_SetInteger( p_input, "state", i_int );
117 case INPUT_GET_AUDIO_DELAY:
118 pi_64 = (int64_t*)va_arg( args, int64_t * );
119 *pi_64 = var_GetTime( p_input, "audio-delay" );
122 case INPUT_GET_SPU_DELAY:
123 pi_64 = (int64_t*)va_arg( args, int64_t * );
124 *pi_64 = var_GetTime( p_input, "spu-delay" );
127 case INPUT_SET_AUDIO_DELAY:
128 i_64 = (int64_t)va_arg( args, int64_t );
129 return var_SetTime( p_input, "audio-delay", i_64 );
131 case INPUT_SET_SPU_DELAY:
132 i_64 = (int64_t)va_arg( args, int64_t );
133 return var_SetTime( p_input, "spu-delay", i_64 );
137 char *psz_cat = (char *)va_arg( args, char * );
138 char *psz_name = (char *)va_arg( args, char * );
139 char *psz_format = (char *)va_arg( args, char * );
143 if( vasprintf( &psz_value, psz_format, args ) == -1 )
146 int i_ret = input_item_AddInfo( p_input->p->p_item,
147 psz_cat, psz_name, "%s", psz_value );
150 if( !p_input->b_preparsing && !i_ret )
151 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 );
171 char *psz_cat = (char *)va_arg( args, char * );
172 char *psz_name = (char *)va_arg( args, char * );
173 char **ppsz_value = (char **)va_arg( args, char ** );
174 int i_ret = VLC_EGENERIC;
177 *ppsz_value = input_item_GetInfo( p_input->p->p_item,
184 char *psz_name = (char *)va_arg( args, char * );
186 if( !psz_name ) return VLC_EGENERIC;
188 input_item_SetName( p_input->p->p_item, psz_name );
190 if( !p_input->b_preparsing )
191 input_SendEventMetaName( p_input, psz_name );
195 case INPUT_ADD_BOOKMARK:
196 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
197 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
199 vlc_mutex_lock( &p_input->p->p_item->lock );
200 if( !p_bkmk->psz_name )
202 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
203 p_input->p->i_bookmark ) == -1 )
204 p_bkmk->psz_name = NULL;
207 TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
209 /* Reflect the changes on the object var */
210 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
212 vlc_value_t val, text;
215 for( i = 0; i < p_input->p->i_bookmark; i++ )
218 text.psz_string = p_input->p->bookmark[i]->psz_name;
219 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
223 vlc_mutex_unlock( &p_input->p->p_item->lock );
225 UpdateBookmarksOption( p_input );
229 case INPUT_CHANGE_BOOKMARK:
230 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
231 i_bkmk = (int)va_arg( args, int );
233 vlc_mutex_lock( &p_input->p->p_item->lock );
234 if( i_bkmk < p_input->p->i_bookmark )
236 vlc_value_t val, text;
239 p_input->p->bookmark[i_bkmk] = p_bkmk;
241 /* Reflect the changes on the object var */
242 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
243 for( i = 0; i < p_input->p->i_bookmark; i++ )
246 text.psz_string = p_input->p->bookmark[i]->psz_name;
247 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
251 vlc_mutex_unlock( &p_input->p->p_item->lock );
253 UpdateBookmarksOption( p_input );
257 case INPUT_DEL_BOOKMARK:
258 i_bkmk = (int)va_arg( args, int );
260 vlc_mutex_lock( &p_input->p->p_item->lock );
261 if( i_bkmk < p_input->p->i_bookmark )
263 vlc_value_t val, text;
266 p_bkmk = p_input->p->bookmark[i_bkmk];
267 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
269 vlc_seekpoint_Delete( p_bkmk );
271 /* Reflect the changes on the object var */
272 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
273 for( i = 0; i < p_input->p->i_bookmark; i++ )
276 text.psz_string = p_input->p->bookmark[i]->psz_name;
277 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
280 vlc_mutex_unlock( &p_input->p->p_item->lock );
282 UpdateBookmarksOption( p_input );
286 vlc_mutex_unlock( &p_input->p->p_item->lock );
290 case INPUT_GET_BOOKMARKS:
291 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
292 pi_bkmk = (int *)va_arg( args, int * );
294 vlc_mutex_lock( &p_input->p->p_item->lock );
295 if( p_input->p->i_bookmark )
299 *pi_bkmk = p_input->p->i_bookmark;
300 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
301 p_input->p->i_bookmark );
302 for( i = 0; i < p_input->p->i_bookmark; i++ )
305 vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
308 vlc_mutex_unlock( &p_input->p->p_item->lock );
316 vlc_mutex_unlock( &p_input->p->p_item->lock );
321 case INPUT_CLEAR_BOOKMARKS:
323 vlc_mutex_lock( &p_input->p->p_item->lock );
324 if( p_input->p->i_bookmark )
328 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
330 p_bkmk = p_input->p->bookmark[i];
331 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
333 vlc_seekpoint_Delete( p_bkmk );
335 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
337 vlc_mutex_unlock( &p_input->p->p_item->lock );
339 UpdateBookmarksOption( p_input );
343 case INPUT_SET_BOOKMARK:
344 i_bkmk = (int)va_arg( args, int );
346 vlc_mutex_lock( &p_input->p->p_item->lock );
347 if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
352 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
354 pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
355 i_ret = var_Set( p_input, "time", pos );
357 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
359 // don't crash on bookmarks in live streams
360 if( stream_Size( p_input->p->input.p_stream ) == 0 )
362 vlc_mutex_unlock( &p_input->p->p_item->lock );
365 pos.f_float = !p_input->p->input.p_stream ? 0 :
366 p_input->p->bookmark[i_bkmk]->i_byte_offset /
367 stream_Size( p_input->p->input.p_stream );
368 i_ret = var_Set( p_input, "position", pos );
373 i_ret = var_Set( p_input, "position", pos );
376 vlc_mutex_unlock( &p_input->p->p_item->lock );
381 vlc_mutex_unlock( &p_input->p->p_item->lock );
387 case INPUT_ADD_OPTION:
389 const char *psz_option = va_arg( args, const char * );
390 const char *psz_value = va_arg( args, const char * );
394 if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
397 i = input_item_AddOpt( p_input->p->p_item, str,
398 VLC_INPUT_OPTION_UNIQUE );
403 case INPUT_GET_BYTE_POSITION:
404 pi_64 = (int64_t*)va_arg( args, int64_t * );
405 *pi_64 = !p_input->p->input.p_stream ? 0 :
406 stream_Tell( p_input->p->input.p_stream );
409 case INPUT_SET_BYTE_SIZE:
410 pi_64 = (int64_t*)va_arg( args, int64_t * );
411 *pi_64 = !p_input->p->input.p_stream ? 0 :
412 stream_Size( p_input->p->input.p_stream );
415 case INPUT_GET_VIDEO_FPS:
418 pf = (double*)va_arg( args, double * );
419 vlc_mutex_lock( &p_input->p->p_item->lock );
420 *pf = p_input->p->input.f_fps;
421 for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
422 *pf = p_input->p->slave[i]->f_fps;
423 vlc_mutex_unlock( &p_input->p->p_item->lock );
427 case INPUT_ADD_SLAVE:
428 psz = (char*)va_arg( args, char * );
431 val.psz_string = strdup( psz );
432 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
436 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
438 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
439 int *pi_attachment = (int*)va_arg( args, int * );
442 vlc_mutex_lock( &p_input->p->p_item->lock );
443 if( p_input->p->i_attachment <= 0 )
445 vlc_mutex_unlock( &p_input->p->p_item->lock );
446 *ppp_attachment = NULL;
450 *pi_attachment = p_input->p->i_attachment;
451 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
452 for( i = 0; i < p_input->p->i_attachment; i++ )
453 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
455 vlc_mutex_unlock( &p_input->p->p_item->lock );
459 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
461 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
462 const char *psz_name = (const char*)va_arg( args, const char * );
465 vlc_mutex_lock( &p_input->p->p_item->lock );
466 for( i = 0; i < p_input->p->i_attachment; i++ )
468 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
470 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
471 vlc_mutex_unlock( &p_input->p->p_item->lock );
475 *pp_attachment = NULL;
476 vlc_mutex_unlock( &p_input->p->p_item->lock );
480 case INPUT_SET_RECORD_STATE:
481 b_bool = (bool)va_arg( args, int );
482 var_SetBool( p_input, "record", b_bool );
485 case INPUT_GET_RECORD_STATE:
486 pb_bool = (bool*)va_arg( args, bool* );
487 *pb_bool = var_GetBool( p_input, "record" );
490 case INPUT_RESTART_ES:
491 val.i_int = (int)va_arg( args, int );
492 input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
496 msg_Err( p_input, "unknown query in input_vaControl" );
501 static void UpdateBookmarksOption( input_thread_t *p_input )
504 char *psz_value = NULL, *psz_next = NULL;
506 vlc_mutex_lock( &p_input->p->p_item->lock );
507 if( p_input->p->i_bookmark > 0 )
509 for( i = 0; i < p_input->p->i_bookmark; i++ )
511 i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
512 p_input->p->bookmark[i]->psz_name,
513 p_input->p->bookmark[i]->i_byte_offset,
514 p_input->p->bookmark[i]->i_time_offset/1000000 );
516 psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
518 for( i = 0; i < p_input->p->i_bookmark; i++ )
520 sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
521 p_input->p->bookmark[i]->psz_name,
522 p_input->p->bookmark[i]->i_byte_offset,
523 p_input->p->bookmark[i]->i_time_offset/1000000 );
525 psz_next += strlen( psz_next );
526 if( i < p_input->p->i_bookmark - 1)
527 *psz_next = ','; psz_next++;
530 vlc_mutex_unlock( &p_input->p->p_item->lock );
532 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
533 psz_value ? psz_value : "" );