More mailmap fixes
[vlc.git] / src / input / control.c
blobdcf723c0dd1f13acf0cc867b11f5f3668d583d8b
1 /*****************************************************************************
2 * control.c
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <assert.h>
34 #include "input_internal.h"
35 #include "event.h"
36 #include "resource.h"
37 #include "es_out.h"
40 static void UpdateBookmarksOption( input_thread_t * );
42 /****************************************************************************
43 * input_Control
44 ****************************************************************************/
45 /**
46 * Control function for inputs.
47 * \param p_input input handle
48 * \param i_query query type
49 * \return VLC_SUCCESS if ok
51 int input_Control( input_thread_t *p_input, int i_query, ... )
53 va_list args;
54 int i_result;
56 va_start( args, i_query );
57 i_result = input_vaControl( p_input, i_query, args );
58 va_end( args );
60 return i_result;
63 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
65 seekpoint_t *p_bkmk, ***ppp_bkmk;
66 int i_bkmk = 0;
67 int *pi_bkmk;
69 int i_int, *pi_int;
70 bool b_bool, *pb_bool;
71 double f, *pf;
72 int64_t i_64, *pi_64;
74 char *psz;
75 vlc_value_t val;
77 switch( i_query )
79 case INPUT_GET_POSITION:
80 pf = (double*)va_arg( args, double * );
81 *pf = var_GetFloat( p_input, "position" );
82 return VLC_SUCCESS;
84 case INPUT_SET_POSITION:
85 f = (double)va_arg( args, double );
86 return var_SetFloat( p_input, "position", f );
88 case INPUT_GET_LENGTH:
89 pi_64 = (int64_t*)va_arg( args, int64_t * );
90 *pi_64 = var_GetTime( p_input, "length" );
91 return VLC_SUCCESS;
93 case INPUT_GET_TIME:
94 pi_64 = (int64_t*)va_arg( args, int64_t * );
95 *pi_64 = var_GetTime( p_input, "time" );
96 return VLC_SUCCESS;
98 case INPUT_SET_TIME:
99 i_64 = (int64_t)va_arg( args, int64_t );
100 return var_SetTime( p_input, "time", i_64 );
102 case INPUT_GET_RATE:
103 pi_int = (int*)va_arg( args, int * );
104 *pi_int = INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" );
105 return VLC_SUCCESS;
107 case INPUT_SET_RATE:
108 i_int = (int)va_arg( args, int );
109 return var_SetFloat( p_input, "rate",
110 (float)INPUT_RATE_DEFAULT / (float)i_int );
112 case INPUT_GET_STATE:
113 pi_int = (int*)va_arg( args, int * );
114 *pi_int = var_GetInteger( p_input, "state" );
115 return VLC_SUCCESS;
117 case INPUT_SET_STATE:
118 i_int = (int)va_arg( args, int );
119 return var_SetInteger( p_input, "state", i_int );
121 case INPUT_GET_AUDIO_DELAY:
122 pi_64 = (int64_t*)va_arg( args, int64_t * );
123 *pi_64 = var_GetTime( p_input, "audio-delay" );
124 return VLC_SUCCESS;
126 case INPUT_GET_SPU_DELAY:
127 pi_64 = (int64_t*)va_arg( args, int64_t * );
128 *pi_64 = var_GetTime( p_input, "spu-delay" );
129 return VLC_SUCCESS;
131 case INPUT_SET_AUDIO_DELAY:
132 i_64 = (int64_t)va_arg( args, int64_t );
133 return var_SetTime( p_input, "audio-delay", i_64 );
135 case INPUT_SET_SPU_DELAY:
136 i_64 = (int64_t)va_arg( args, int64_t );
137 return var_SetTime( p_input, "spu-delay", i_64 );
139 case INPUT_ADD_INFO:
141 char *psz_cat = (char *)va_arg( args, char * );
142 char *psz_name = (char *)va_arg( args, char * );
143 char *psz_format = (char *)va_arg( args, char * );
145 char *psz_value;
147 if( vasprintf( &psz_value, psz_format, args ) == -1 )
148 return VLC_EGENERIC;
150 int i_ret = input_item_AddInfo( p_input->p->p_item,
151 psz_cat, psz_name, "%s", psz_value );
152 free( psz_value );
154 if( !p_input->b_preparsing && !i_ret )
155 input_SendEventMetaInfo( p_input );
156 return i_ret;
158 case INPUT_REPLACE_INFOS:
159 case INPUT_MERGE_INFOS:
161 info_category_t *p_cat = va_arg( args, info_category_t * );
163 if( i_query == INPUT_REPLACE_INFOS )
164 input_item_ReplaceInfos( p_input->p->p_item, p_cat );
165 else
166 input_item_MergeInfos( p_input->p->p_item, p_cat );
168 if( !p_input->b_preparsing )
169 input_SendEventMetaInfo( p_input );
170 return VLC_SUCCESS;
172 case INPUT_DEL_INFO:
174 char *psz_cat = (char *)va_arg( args, char * );
175 char *psz_name = (char *)va_arg( args, char * );
177 int i_ret = input_item_DelInfo( p_input->p->p_item,
178 psz_cat, psz_name );
180 if( !p_input->b_preparsing && !i_ret )
181 input_SendEventMetaInfo( p_input );
182 return i_ret;
184 case INPUT_GET_INFO:
186 char *psz_cat = (char *)va_arg( args, char * );
187 char *psz_name = (char *)va_arg( args, char * );
188 char **ppsz_value = (char **)va_arg( args, char ** );
189 int i_ret = VLC_EGENERIC;
190 *ppsz_value = NULL;
192 *ppsz_value = input_item_GetInfo( p_input->p->p_item,
193 psz_cat, psz_name );
194 return i_ret;
197 case INPUT_SET_NAME:
199 char *psz_name = (char *)va_arg( args, char * );
201 if( !psz_name ) return VLC_EGENERIC;
203 input_item_SetName( p_input->p->p_item, psz_name );
205 if( !p_input->b_preparsing )
206 input_SendEventMetaName( p_input, psz_name );
207 return VLC_SUCCESS;
210 case INPUT_ADD_BOOKMARK:
211 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
212 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
214 vlc_mutex_lock( &p_input->p->p_item->lock );
215 if( !p_bkmk->psz_name )
217 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
218 p_input->p->i_bookmark ) == -1 )
219 p_bkmk->psz_name = NULL;
222 TAB_APPEND( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
223 vlc_mutex_unlock( &p_input->p->p_item->lock );
225 UpdateBookmarksOption( p_input );
227 return VLC_SUCCESS;
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_seekpoint_Delete( p_input->p->pp_bookmark[i_bkmk] );
237 p_input->p->pp_bookmark[i_bkmk] = vlc_seekpoint_Duplicate( p_bkmk );
239 vlc_mutex_unlock( &p_input->p->p_item->lock );
241 UpdateBookmarksOption( p_input );
243 return VLC_SUCCESS;
245 case INPUT_DEL_BOOKMARK:
246 i_bkmk = (int)va_arg( args, int );
248 vlc_mutex_lock( &p_input->p->p_item->lock );
249 if( i_bkmk < p_input->p->i_bookmark )
251 p_bkmk = p_input->p->pp_bookmark[i_bkmk];
252 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
253 vlc_seekpoint_Delete( p_bkmk );
255 vlc_mutex_unlock( &p_input->p->p_item->lock );
257 UpdateBookmarksOption( p_input );
259 return VLC_SUCCESS;
261 vlc_mutex_unlock( &p_input->p->p_item->lock );
263 return VLC_EGENERIC;
265 case INPUT_GET_BOOKMARKS:
266 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
267 pi_bkmk = (int *)va_arg( args, int * );
269 vlc_mutex_lock( &p_input->p->p_item->lock );
270 if( p_input->p->i_bookmark )
272 int i;
274 *pi_bkmk = p_input->p->i_bookmark;
275 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
276 p_input->p->i_bookmark );
277 for( i = 0; i < p_input->p->i_bookmark; i++ )
279 (*ppp_bkmk)[i] =
280 vlc_seekpoint_Duplicate( p_input->p->pp_bookmark[i] );
283 vlc_mutex_unlock( &p_input->p->p_item->lock );
284 return VLC_SUCCESS;
286 else
288 *ppp_bkmk = NULL;
289 *pi_bkmk = 0;
291 vlc_mutex_unlock( &p_input->p->p_item->lock );
292 return VLC_EGENERIC;
294 break;
296 case INPUT_CLEAR_BOOKMARKS:
298 vlc_mutex_lock( &p_input->p->p_item->lock );
299 while( p_input->p->i_bookmark > 0 )
301 p_bkmk = p_input->p->pp_bookmark[p_input->p->i_bookmark-1];
303 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark,
304 p_bkmk );
305 vlc_seekpoint_Delete( p_bkmk );
307 vlc_mutex_unlock( &p_input->p->p_item->lock );
309 UpdateBookmarksOption( p_input );
310 return VLC_SUCCESS;
312 case INPUT_SET_BOOKMARK:
313 i_bkmk = (int)va_arg( args, int );
315 val.i_int = i_bkmk;
316 input_ControlPush( p_input, INPUT_CONTROL_SET_BOOKMARK, &val );
318 return VLC_SUCCESS;
320 case INPUT_GET_BOOKMARK:
321 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
323 vlc_mutex_lock( &p_input->p->p_item->lock );
324 *p_bkmk = p_input->p->bookmark;
325 vlc_mutex_unlock( &p_input->p->p_item->lock );
326 return VLC_SUCCESS;
328 case INPUT_GET_TITLE_INFO:
330 input_title_t **p_title = (input_title_t **)va_arg( args, input_title_t ** );
331 int *pi_req_title_offset = (int *) va_arg( args, int * );
333 vlc_mutex_lock( &p_input->p->p_item->lock );
335 int i_current_title = var_GetInteger( p_input, "title" );
336 if ( *pi_req_title_offset < 0 ) /* return current title if -1 */
337 *pi_req_title_offset = i_current_title;
339 if( p_input->p->i_title && p_input->p->i_title > *pi_req_title_offset )
341 *p_title = vlc_input_title_Duplicate( p_input->p->title[*pi_req_title_offset] );
342 vlc_mutex_unlock( &p_input->p->p_item->lock );
343 return VLC_SUCCESS;
345 else
347 vlc_mutex_unlock( &p_input->p->p_item->lock );
348 return VLC_EGENERIC;
352 case INPUT_GET_VIDEO_FPS:
353 pf = (double*)va_arg( args, double * );
355 vlc_mutex_lock( &p_input->p->p_item->lock );
356 *pf = p_input->p->f_fps;
357 vlc_mutex_unlock( &p_input->p->p_item->lock );
358 return VLC_SUCCESS;
360 case INPUT_ADD_SLAVE:
361 psz = (char*)va_arg( args, char * );
362 if( psz && *psz )
364 val.psz_string = strdup( psz );
365 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
367 return VLC_SUCCESS;
369 case INPUT_ADD_SUBTITLE:
370 psz = (char*)va_arg( args, char * );
371 b_bool = (bool)va_arg( args, int );
373 if( !psz || *psz == '\0' )
374 return VLC_EGENERIC;
375 if( b_bool && !subtitles_Filter( psz ) )
376 return VLC_EGENERIC;
378 val.psz_string = strdup( psz );
379 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
380 return VLC_SUCCESS;
382 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
384 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
385 int *pi_attachment = (int*)va_arg( args, int * );
386 int i;
388 vlc_mutex_lock( &p_input->p->p_item->lock );
389 if( p_input->p->i_attachment <= 0 )
391 vlc_mutex_unlock( &p_input->p->p_item->lock );
392 *ppp_attachment = NULL;
393 *pi_attachment = 0;
394 return VLC_EGENERIC;
396 *pi_attachment = p_input->p->i_attachment;
397 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
398 for( i = 0; i < p_input->p->i_attachment; i++ )
399 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
401 vlc_mutex_unlock( &p_input->p->p_item->lock );
402 return VLC_SUCCESS;
405 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
407 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
408 const char *psz_name = (const char*)va_arg( args, const char * );
409 int i;
411 vlc_mutex_lock( &p_input->p->p_item->lock );
412 for( i = 0; i < p_input->p->i_attachment; i++ )
414 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
416 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
417 vlc_mutex_unlock( &p_input->p->p_item->lock );
418 return VLC_SUCCESS;
421 *pp_attachment = NULL;
422 vlc_mutex_unlock( &p_input->p->p_item->lock );
423 return VLC_EGENERIC;
426 case INPUT_SET_RECORD_STATE:
427 b_bool = (bool)va_arg( args, int );
428 var_SetBool( p_input, "record", b_bool );
429 return VLC_SUCCESS;
431 case INPUT_GET_RECORD_STATE:
432 pb_bool = (bool*)va_arg( args, bool* );
433 *pb_bool = var_GetBool( p_input, "record" );
434 return VLC_SUCCESS;
436 case INPUT_RESTART_ES:
437 val.i_int = (int)va_arg( args, int );
438 input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
439 return VLC_SUCCESS;
441 case INPUT_GET_AOUT:
443 audio_output_t *p_aout = input_resource_HoldAout( p_input->p->p_resource );
444 if( !p_aout )
445 return VLC_EGENERIC;
447 audio_output_t **pp_aout = (audio_output_t**)va_arg( args, audio_output_t** );
448 *pp_aout = p_aout;
449 return VLC_SUCCESS;
452 case INPUT_GET_VOUTS:
454 vout_thread_t ***ppp_vout = (vout_thread_t***)va_arg( args, vout_thread_t*** );
455 size_t *pi_vout = va_arg( args, size_t * );
457 input_resource_HoldVouts( p_input->p->p_resource, ppp_vout, pi_vout );
458 if( *pi_vout <= 0 )
459 return VLC_EGENERIC;
460 return VLC_SUCCESS;
463 case INPUT_GET_ES_OBJECTS:
465 const int i_id = va_arg( args, int );
466 vlc_object_t **pp_decoder = va_arg( args, vlc_object_t ** );
467 vout_thread_t **pp_vout = va_arg( args, vout_thread_t ** );
468 audio_output_t **pp_aout = va_arg( args, audio_output_t ** );
470 return es_out_Control( p_input->p->p_es_out_display, ES_OUT_GET_ES_OBJECTS_BY_ID, i_id,
471 pp_decoder, pp_vout, pp_aout );
474 case INPUT_GET_PCR_SYSTEM:
476 mtime_t *pi_system = va_arg( args, mtime_t * );
477 mtime_t *pi_delay = va_arg( args, mtime_t * );
478 return es_out_ControlGetPcrSystem( p_input->p->p_es_out_display, pi_system, pi_delay );
481 case INPUT_MODIFY_PCR_SYSTEM:
483 bool b_absolute = va_arg( args, int );
484 mtime_t i_system = va_arg( args, mtime_t );
485 return es_out_ControlModifyPcrSystem( p_input->p->p_es_out_display, b_absolute, i_system );
488 default:
489 msg_Err( p_input, "unknown query in input_vaControl" );
490 return VLC_EGENERIC;
494 static void UpdateBookmarksOption( input_thread_t *p_input )
496 vlc_mutex_lock( &p_input->p->p_item->lock );
498 /* Update the "bookmark" list */
499 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
500 for( int i = 0; i < p_input->p->i_bookmark; i++ )
502 vlc_value_t val, text;
504 val.i_int = i;
505 text.psz_string = p_input->p->pp_bookmark[i]->psz_name;
506 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
507 &val, &text );
510 /* Create the "bookmarks" option value */
511 const char *psz_format = "{name=%s,bytes=%"PRId64",time=%"PRId64"}";
512 int i_len = strlen( "bookmarks=" );
513 for( int i = 0; i < p_input->p->i_bookmark; i++ )
515 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
517 i_len += snprintf( NULL, 0, psz_format,
518 p_bookmark->psz_name,
519 p_bookmark->i_byte_offset,
520 p_bookmark->i_time_offset/1000000 );
523 char *psz_value = malloc( i_len + p_input->p->i_bookmark + 1 );
524 char *psz_next = psz_value;
526 psz_next += sprintf( psz_next, "bookmarks=" );
527 for( int i = 0; i < p_input->p->i_bookmark && psz_value != NULL; i++ )
529 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
531 psz_next += sprintf( psz_next, psz_format,
532 p_bookmark->psz_name,
533 p_bookmark->i_byte_offset,
534 p_bookmark->i_time_offset/1000000 );
536 if( i < p_input->p->i_bookmark - 1)
537 *psz_next++ = ',';
539 vlc_mutex_unlock( &p_input->p->p_item->lock );
541 if( psz_value )
542 input_item_AddOption( p_input->p->p_item, psz_value, VLC_INPUT_OPTION_UNIQUE );
544 free( psz_value );
546 input_SendEventBookmark( p_input );