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>
34 #include "input_internal.h"
40 static void UpdateBookmarksOption( input_thread_t
* );
42 /****************************************************************************
44 ****************************************************************************/
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
, ... )
56 va_start( args
, i_query
);
57 i_result
= input_vaControl( p_input
, i_query
, args
);
63 int input_vaControl( input_thread_t
*p_input
, int i_query
, va_list args
)
65 seekpoint_t
*p_bkmk
, ***ppp_bkmk
;
70 bool b_bool
, *pb_bool
;
79 case INPUT_GET_POSITION
:
80 pf
= (double*)va_arg( args
, double * );
81 *pf
= var_GetFloat( p_input
, "position" );
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" );
94 pi_64
= (int64_t*)va_arg( args
, int64_t * );
95 *pi_64
= var_GetTime( p_input
, "time" );
99 i_64
= (int64_t)va_arg( args
, int64_t );
100 return var_SetTime( p_input
, "time", i_64
);
103 pi_int
= (int*)va_arg( args
, int * );
104 *pi_int
= INPUT_RATE_DEFAULT
/ var_GetFloat( p_input
, "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" );
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" );
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" );
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
);
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 * );
147 if( vasprintf( &psz_value
, psz_format
, args
) == -1 )
150 int i_ret
= input_item_AddInfo( p_input
->p
->p_item
,
151 psz_cat
, psz_name
, "%s", psz_value
);
154 if( !p_input
->b_preparsing
&& !i_ret
)
155 input_SendEventMetaInfo( p_input
);
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
);
166 input_item_MergeInfos( p_input
->p
->p_item
, p_cat
);
168 if( !p_input
->b_preparsing
)
169 input_SendEventMetaInfo( p_input
);
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
,
180 if( !p_input
->b_preparsing
&& !i_ret
)
181 input_SendEventMetaInfo( p_input
);
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
;
192 *ppsz_value
= input_item_GetInfo( p_input
->p
->p_item
,
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
);
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
);
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
);
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
);
261 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
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
)
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
++ )
280 vlc_seekpoint_Duplicate( p_input
->p
->pp_bookmark
[i
] );
283 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
291 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
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
,
305 vlc_seekpoint_Delete( p_bkmk
);
307 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
309 UpdateBookmarksOption( p_input
);
312 case INPUT_SET_BOOKMARK
:
313 i_bkmk
= (int)va_arg( args
, int );
316 input_ControlPush( p_input
, INPUT_CONTROL_SET_BOOKMARK
, &val
);
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
);
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
);
347 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
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
);
360 case INPUT_ADD_SLAVE
:
361 psz
= (char*)va_arg( args
, char * );
364 val
.psz_string
= strdup( psz
);
365 input_ControlPush( p_input
, INPUT_CONTROL_ADD_SLAVE
, &val
);
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' )
375 if( b_bool
&& !subtitles_Filter( psz
) )
378 val
.psz_string
= strdup( psz
);
379 input_ControlPush( p_input
, INPUT_CONTROL_ADD_SUBTITLE
, &val
);
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 * );
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
;
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
);
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 * );
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
);
421 *pp_attachment
= NULL
;
422 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
426 case INPUT_SET_RECORD_STATE
:
427 b_bool
= (bool)va_arg( args
, int );
428 var_SetBool( p_input
, "record", b_bool
);
431 case INPUT_GET_RECORD_STATE
:
432 pb_bool
= (bool*)va_arg( args
, bool* );
433 *pb_bool
= var_GetBool( p_input
, "record" );
436 case INPUT_RESTART_ES
:
437 val
.i_int
= (int)va_arg( args
, int );
438 input_ControlPush( p_input
, INPUT_CONTROL_RESTART_ES
, &val
);
443 audio_output_t
*p_aout
= input_resource_HoldAout( p_input
->p
->p_resource
);
447 audio_output_t
**pp_aout
= (audio_output_t
**)va_arg( args
, audio_output_t
** );
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
);
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
);
489 msg_Err( p_input
, "unknown query in input_vaControl" );
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
;
505 text
.psz_string
= p_input
->p
->pp_bookmark
[i
]->psz_name
;
506 var_Change( p_input
, "bookmark", VLC_VAR_ADDCHOICE
,
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)
539 vlc_mutex_unlock( &p_input
->p
->p_item
->lock
);
542 input_item_AddOption( p_input
->p
->p_item
, psz_value
, VLC_INPUT_OPTION_UNIQUE
);
546 input_SendEventBookmark( p_input
);