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"
36 static void UpdateBookmarksOption( input_thread_t * );
38 /****************************************************************************
40 ****************************************************************************/
42 * Control function for inputs.
43 * \param p_input input handle
44 * \param i_query query type
45 * \return VLC_SUCCESS if ok
47 int input_Control( input_thread_t *p_input, int i_query, ... )
52 va_start( args, i_query );
53 i_result = input_vaControl( p_input, i_query, args );
59 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
61 seekpoint_t *p_bkmk, ***ppp_bkmk;
74 case INPUT_GET_POSITION:
75 pf = (double*)va_arg( args, double * );
76 *pf = var_GetFloat( p_input, "position" );
79 case INPUT_SET_POSITION:
80 f = (double)va_arg( args, double );
81 return var_SetFloat( p_input, "position", f );
83 case INPUT_GET_LENGTH:
84 pi_64 = (int64_t*)va_arg( args, int64_t * );
85 *pi_64 = var_GetTime( p_input, "length" );
89 pi_64 = (int64_t*)va_arg( args, int64_t * );
90 *pi_64 = var_GetTime( p_input, "time" );
94 i_64 = (int64_t)va_arg( args, int64_t );
95 return var_SetTime( p_input, "time", i_64 );
98 pi_int = (int*)va_arg( args, int * );
99 *pi_int = var_GetInteger( p_input, "rate" );
103 i_int = (int)va_arg( args, int );
104 return var_SetInteger( p_input, "rate", i_int );
106 case INPUT_GET_STATE:
107 pi_int = (int*)va_arg( args, int * );
108 *pi_int = var_GetInteger( p_input, "state" );
111 case INPUT_SET_STATE:
112 i_int = (int)va_arg( args, int );
113 return var_SetInteger( p_input, "state", i_int );
115 case INPUT_GET_AUDIO_DELAY:
116 pi_64 = (int64_t*)va_arg( args, int64_t * );
117 *pi_64 = var_GetTime( p_input, "audio-delay" );
120 case INPUT_GET_SPU_DELAY:
121 pi_64 = (int64_t*)va_arg( args, int64_t * );
122 *pi_64 = var_GetTime( p_input, "spu-delay" );
125 case INPUT_SET_AUDIO_DELAY:
126 i_64 = (int64_t)va_arg( args, int64_t );
127 return var_SetTime( p_input, "audio-delay", i_64 );
129 case INPUT_SET_SPU_DELAY:
130 i_64 = (int64_t)va_arg( args, int64_t );
131 return var_SetTime( p_input, "spu-delay", i_64 );
135 /* FIXME : Impossible to use input_ItemAddInfo because of
136 * the ... problem ? */
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 * );
141 info_category_t *p_cat;
145 vlc_mutex_lock( &p_input->p->input.p_item->lock );
146 for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
148 if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
152 if( i == p_input->p->input.p_item->i_categories )
154 p_cat = malloc( sizeof( info_category_t ) );
157 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
160 p_cat->psz_name = strdup( psz_cat );
162 p_cat->pp_infos = NULL;
163 INSERT_ELEM( p_input->p->input.p_item->pp_categories,
164 p_input->p->input.p_item->i_categories,
165 p_input->p->input.p_item->i_categories, p_cat );
168 p_cat = p_input->p->input.p_item->pp_categories[i];
170 for( i = 0; i < p_cat->i_infos; i++ )
172 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
174 if( p_cat->pp_infos[i]->psz_value )
175 free( p_cat->pp_infos[i]->psz_value );
180 if( i == p_cat->i_infos )
182 p_info = malloc( sizeof( info_t ) );
185 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
189 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
190 p_cat->i_infos, p_info );
191 p_info->psz_name = strdup( psz_name );
194 p_info = p_cat->pp_infos[i];
195 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
196 p_info->psz_value = NULL;
198 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
200 if( !p_input->b_preparsing )
203 event.type = vlc_InputItemInfoChanged;
204 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
211 char *psz_cat = (char *)va_arg( args, char * );
212 char *psz_name = (char *)va_arg( args, char * );
214 info_category_t *p_cat = NULL;
218 vlc_mutex_lock( &p_input->p->input.p_item->lock );
219 for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
221 if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
224 p_cat = p_input->p->input.p_item->pp_categories[i_cat];
230 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
236 /* Remove a specific info */
237 for( i = 0; i < p_cat->i_infos; i++ )
239 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
241 free( p_cat->pp_infos[i]->psz_name );
242 if( p_cat->pp_infos[i]->psz_value )
243 free( p_cat->pp_infos[i]->psz_value );
244 free( p_cat->pp_infos[i] );
245 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
249 if( i >= p_cat->i_infos )
251 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
257 /* Remove the complete categorie */
258 for( i = 0; i < p_cat->i_infos; i++ )
260 free( p_cat->pp_infos[i]->psz_name );
261 if( p_cat->pp_infos[i]->psz_value )
262 free( p_cat->pp_infos[i]->psz_value );
263 free( p_cat->pp_infos[i] );
265 if( p_cat->pp_infos )
266 free( p_cat->pp_infos );
267 REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
269 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
271 if( !p_input->b_preparsing )
274 event.type = vlc_InputItemInfoChanged;
275 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
283 char *psz_cat = (char *)va_arg( args, char * );
284 char *psz_name = (char *)va_arg( args, char * );
285 char **ppsz_value = (char **)va_arg( args, char ** );
286 int i_ret = VLC_EGENERIC;
289 *ppsz_value = input_ItemGetInfo( p_input->p->input.p_item,
296 char *psz_name = (char *)va_arg( args, char * );
298 if( !psz_name ) return VLC_EGENERIC;
300 vlc_mutex_lock( &p_input->p->input.p_item->lock );
301 if( p_input->p->input.p_item->psz_name )
302 free( p_input->p->input.p_item->psz_name );
303 p_input->p->input.p_item->psz_name = strdup( psz_name );
304 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
306 if( !p_input->b_preparsing )
309 event.type = vlc_InputItemNameChanged;
310 event.u.input_item_name_changed.new_name = psz_name;
311 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
316 case INPUT_ADD_BOOKMARK:
317 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
318 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
320 vlc_mutex_lock( &p_input->p->input.p_item->lock );
321 if( !p_bkmk->psz_name )
323 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
324 p_input->p->i_bookmark ) == -1 )
325 p_bkmk->psz_name = NULL;
328 TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
330 /* Reflect the changes on the object var */
331 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
333 vlc_value_t val, text;
336 for( i = 0; i < p_input->p->i_bookmark; i++ )
339 text.psz_string = p_input->p->bookmark[i]->psz_name;
340 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
344 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
346 UpdateBookmarksOption( p_input );
350 case INPUT_CHANGE_BOOKMARK:
351 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
352 i_bkmk = (int)va_arg( args, int );
354 vlc_mutex_lock( &p_input->p->input.p_item->lock );
355 if( i_bkmk < p_input->p->i_bookmark )
357 vlc_value_t val, text;
360 p_input->p->bookmark[i_bkmk] = p_bkmk;
362 /* Reflect the changes on the object var */
363 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
364 for( i = 0; i < p_input->p->i_bookmark; i++ )
367 text.psz_string = p_input->p->bookmark[i]->psz_name;
368 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
372 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
374 UpdateBookmarksOption( p_input );
378 case INPUT_DEL_BOOKMARK:
379 i_bkmk = (int)va_arg( args, int );
381 vlc_mutex_lock( &p_input->p->input.p_item->lock );
382 if( i_bkmk < p_input->p->i_bookmark )
384 vlc_value_t val, text;
387 p_bkmk = p_input->p->bookmark[i_bkmk];
388 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
390 vlc_seekpoint_Delete( p_bkmk );
392 /* Reflect the changes on the object var */
393 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
394 for( i = 0; i < p_input->p->i_bookmark; i++ )
397 text.psz_string = p_input->p->bookmark[i]->psz_name;
398 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
401 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
403 UpdateBookmarksOption( p_input );
407 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
411 case INPUT_GET_BOOKMARKS:
412 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
413 pi_bkmk = (int *)va_arg( args, int * );
415 vlc_mutex_lock( &p_input->p->input.p_item->lock );
416 if( p_input->p->i_bookmark )
420 *pi_bkmk = p_input->p->i_bookmark;
421 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
422 p_input->p->i_bookmark );
423 for( i = 0; i < p_input->p->i_bookmark; i++ )
426 vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
429 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
437 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
442 case INPUT_CLEAR_BOOKMARKS:
444 vlc_mutex_lock( &p_input->p->input.p_item->lock );
445 if( p_input->p->i_bookmark )
449 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
451 p_bkmk = p_input->p->bookmark[i];
452 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
454 vlc_seekpoint_Delete( p_bkmk );
456 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
458 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
460 UpdateBookmarksOption( p_input );
464 case INPUT_SET_BOOKMARK:
465 i_bkmk = (int)va_arg( args, int );
467 vlc_mutex_lock( &p_input->p->input.p_item->lock );
468 if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
473 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
475 pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
476 i_ret = var_Set( p_input, "time", pos );
478 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
480 // don't crash on bookmarks in live streams
481 if( stream_Size( p_input->p->input.p_stream ) == 0 )
483 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
486 pos.f_float = !p_input->p->input.p_stream ? 0 :
487 p_input->p->bookmark[i_bkmk]->i_byte_offset /
488 stream_Size( p_input->p->input.p_stream );
489 i_ret = var_Set( p_input, "position", pos );
494 i_ret = var_Set( p_input, "position", pos );
497 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
502 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
508 case INPUT_ADD_OPTION:
510 const char *psz_option = va_arg( args, const char * );
511 const char *psz_value = va_arg( args, const char * );
515 if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
518 i = input_ItemAddOpt( p_input->p->input.p_item, str,
519 VLC_INPUT_OPTION_UNIQUE );
524 case INPUT_GET_BYTE_POSITION:
525 pi_64 = (int64_t*)va_arg( args, int64_t * );
526 *pi_64 = !p_input->p->input.p_stream ? 0 :
527 stream_Tell( p_input->p->input.p_stream );
530 case INPUT_SET_BYTE_SIZE:
531 pi_64 = (int64_t*)va_arg( args, int64_t * );
532 *pi_64 = !p_input->p->input.p_stream ? 0 :
533 stream_Size( p_input->p->input.p_stream );
536 case INPUT_GET_VIDEO_FPS:
539 pf = (double*)va_arg( args, double * );
540 vlc_mutex_lock( &p_input->p->input.p_item->lock );
541 *pf = p_input->p->input.f_fps;
542 for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
543 *pf = p_input->p->slave[i]->f_fps;
544 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
548 case INPUT_ADD_SLAVE:
549 psz = (char*)va_arg( args, char * );
552 val.psz_string = strdup( psz );
553 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
557 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
559 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
560 int *pi_attachment = (int*)va_arg( args, int * );
563 vlc_mutex_lock( &p_input->p->input.p_item->lock );
564 if( p_input->p->i_attachment <= 0 )
566 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
567 *ppp_attachment = NULL;
571 *pi_attachment = p_input->p->i_attachment;
572 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
573 for( i = 0; i < p_input->p->i_attachment; i++ )
574 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
576 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
580 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
582 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
583 const char *psz_name = (const char*)va_arg( args, const char * );
586 vlc_mutex_lock( &p_input->p->input.p_item->lock );
587 for( i = 0; i < p_input->p->i_attachment; i++ )
589 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
591 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
592 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
596 *pp_attachment = NULL;
597 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
603 msg_Err( p_input, "unknown query in input_vaControl" );
608 static void UpdateBookmarksOption( input_thread_t *p_input )
611 char *psz_value = NULL, *psz_next = NULL;
613 vlc_mutex_lock( &p_input->p->input.p_item->lock );
614 for( i = 0; i < p_input->p->i_bookmark; i++ )
616 i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
617 p_input->p->bookmark[i]->psz_name,
618 p_input->p->bookmark[i]->i_byte_offset,
619 p_input->p->bookmark[i]->i_time_offset/1000000 );
621 for( i = 0; i < p_input->p->i_bookmark; i++ )
623 if( !i ) psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
625 sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
626 p_input->p->bookmark[i]->psz_name,
627 p_input->p->bookmark[i]->i_byte_offset,
628 p_input->p->bookmark[i]->i_time_offset/1000000 );
630 psz_next += strlen( psz_next );
631 if( i < p_input->p->i_bookmark - 1)
633 *psz_next = ','; psz_next++;
636 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
638 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
639 psz_value ? psz_value : "" );