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;
66 bool b_bool, *pb_bool;
75 case INPUT_GET_POSITION:
76 pf = (double*)va_arg( args, double * );
77 *pf = var_GetFloat( p_input, "position" );
80 case INPUT_SET_POSITION:
81 f = (double)va_arg( args, double );
82 return var_SetFloat( p_input, "position", f );
84 case INPUT_GET_LENGTH:
85 pi_64 = (int64_t*)va_arg( args, int64_t * );
86 *pi_64 = var_GetTime( p_input, "length" );
90 pi_64 = (int64_t*)va_arg( args, int64_t * );
91 *pi_64 = var_GetTime( p_input, "time" );
95 i_64 = (int64_t)va_arg( args, int64_t );
96 return var_SetTime( p_input, "time", i_64 );
99 pi_int = (int*)va_arg( args, int * );
100 *pi_int = var_GetInteger( p_input, "rate" );
104 i_int = (int)va_arg( args, int );
105 return var_SetInteger( p_input, "rate", i_int );
107 case INPUT_GET_STATE:
108 pi_int = (int*)va_arg( args, int * );
109 *pi_int = var_GetInteger( p_input, "state" );
112 case INPUT_SET_STATE:
113 i_int = (int)va_arg( args, int );
114 return var_SetInteger( p_input, "state", i_int );
116 case INPUT_GET_AUDIO_DELAY:
117 pi_64 = (int64_t*)va_arg( args, int64_t * );
118 *pi_64 = var_GetTime( p_input, "audio-delay" );
121 case INPUT_GET_SPU_DELAY:
122 pi_64 = (int64_t*)va_arg( args, int64_t * );
123 *pi_64 = var_GetTime( p_input, "spu-delay" );
126 case INPUT_SET_AUDIO_DELAY:
127 i_64 = (int64_t)va_arg( args, int64_t );
128 return var_SetTime( p_input, "audio-delay", i_64 );
130 case INPUT_SET_SPU_DELAY:
131 i_64 = (int64_t)va_arg( args, int64_t );
132 return var_SetTime( p_input, "spu-delay", i_64 );
136 /* FIXME : Impossible to use input_item_AddInfo because of
137 * the ... problem ? */
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 * );
142 info_category_t *p_cat;
146 vlc_mutex_lock( &p_input->p->input.p_item->lock );
147 for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
149 if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
153 if( i == p_input->p->input.p_item->i_categories )
155 p_cat = malloc( sizeof( info_category_t ) );
158 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
161 p_cat->psz_name = strdup( psz_cat );
163 p_cat->pp_infos = NULL;
164 INSERT_ELEM( p_input->p->input.p_item->pp_categories,
165 p_input->p->input.p_item->i_categories,
166 p_input->p->input.p_item->i_categories, p_cat );
169 p_cat = p_input->p->input.p_item->pp_categories[i];
171 for( i = 0; i < p_cat->i_infos; i++ )
173 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
175 if( p_cat->pp_infos[i]->psz_value )
176 free( p_cat->pp_infos[i]->psz_value );
181 if( i == p_cat->i_infos )
183 p_info = malloc( sizeof( info_t ) );
186 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
190 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
191 p_cat->i_infos, p_info );
192 p_info->psz_name = strdup( psz_name );
195 p_info = p_cat->pp_infos[i];
196 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
197 p_info->psz_value = NULL;
199 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
201 if( !p_input->b_preparsing )
204 event.type = vlc_InputItemInfoChanged;
205 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
212 char *psz_cat = (char *)va_arg( args, char * );
213 char *psz_name = (char *)va_arg( args, char * );
215 info_category_t *p_cat = NULL;
219 vlc_mutex_lock( &p_input->p->input.p_item->lock );
220 for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
222 if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
225 p_cat = p_input->p->input.p_item->pp_categories[i_cat];
231 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
237 /* Remove a specific info */
238 for( i = 0; i < p_cat->i_infos; i++ )
240 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
242 free( p_cat->pp_infos[i]->psz_name );
243 if( p_cat->pp_infos[i]->psz_value )
244 free( p_cat->pp_infos[i]->psz_value );
245 free( p_cat->pp_infos[i] );
246 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
250 if( i >= p_cat->i_infos )
252 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
258 /* Remove the complete categorie */
259 for( i = 0; i < p_cat->i_infos; i++ )
261 free( p_cat->pp_infos[i]->psz_name );
262 if( p_cat->pp_infos[i]->psz_value )
263 free( p_cat->pp_infos[i]->psz_value );
264 free( p_cat->pp_infos[i] );
266 if( p_cat->pp_infos )
267 free( p_cat->pp_infos );
268 REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
270 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
272 if( !p_input->b_preparsing )
275 event.type = vlc_InputItemInfoChanged;
276 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
284 char *psz_cat = (char *)va_arg( args, char * );
285 char *psz_name = (char *)va_arg( args, char * );
286 char **ppsz_value = (char **)va_arg( args, char ** );
287 int i_ret = VLC_EGENERIC;
290 *ppsz_value = input_item_GetInfo( p_input->p->input.p_item,
297 char *psz_name = (char *)va_arg( args, char * );
299 if( !psz_name ) return VLC_EGENERIC;
301 vlc_mutex_lock( &p_input->p->input.p_item->lock );
302 if( p_input->p->input.p_item->psz_name )
303 free( p_input->p->input.p_item->psz_name );
304 p_input->p->input.p_item->psz_name = strdup( psz_name );
305 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
307 if( !p_input->b_preparsing )
310 event.type = vlc_InputItemNameChanged;
311 event.u.input_item_name_changed.new_name = psz_name;
312 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
317 case INPUT_ADD_BOOKMARK:
318 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
319 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
321 vlc_mutex_lock( &p_input->p->input.p_item->lock );
322 if( !p_bkmk->psz_name )
324 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
325 p_input->p->i_bookmark ) == -1 )
326 p_bkmk->psz_name = NULL;
329 TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
331 /* Reflect the changes on the object var */
332 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
334 vlc_value_t val, text;
337 for( i = 0; i < p_input->p->i_bookmark; i++ )
340 text.psz_string = p_input->p->bookmark[i]->psz_name;
341 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
345 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
347 UpdateBookmarksOption( p_input );
351 case INPUT_CHANGE_BOOKMARK:
352 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
353 i_bkmk = (int)va_arg( args, int );
355 vlc_mutex_lock( &p_input->p->input.p_item->lock );
356 if( i_bkmk < p_input->p->i_bookmark )
358 vlc_value_t val, text;
361 p_input->p->bookmark[i_bkmk] = p_bkmk;
363 /* Reflect the changes on the object var */
364 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
365 for( i = 0; i < p_input->p->i_bookmark; i++ )
368 text.psz_string = p_input->p->bookmark[i]->psz_name;
369 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
373 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
375 UpdateBookmarksOption( p_input );
379 case INPUT_DEL_BOOKMARK:
380 i_bkmk = (int)va_arg( args, int );
382 vlc_mutex_lock( &p_input->p->input.p_item->lock );
383 if( i_bkmk < p_input->p->i_bookmark )
385 vlc_value_t val, text;
388 p_bkmk = p_input->p->bookmark[i_bkmk];
389 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
391 vlc_seekpoint_Delete( p_bkmk );
393 /* Reflect the changes on the object var */
394 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
395 for( i = 0; i < p_input->p->i_bookmark; i++ )
398 text.psz_string = p_input->p->bookmark[i]->psz_name;
399 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
402 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
404 UpdateBookmarksOption( p_input );
408 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
412 case INPUT_GET_BOOKMARKS:
413 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
414 pi_bkmk = (int *)va_arg( args, int * );
416 vlc_mutex_lock( &p_input->p->input.p_item->lock );
417 if( p_input->p->i_bookmark )
421 *pi_bkmk = p_input->p->i_bookmark;
422 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
423 p_input->p->i_bookmark );
424 for( i = 0; i < p_input->p->i_bookmark; i++ )
427 vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
430 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
438 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
443 case INPUT_CLEAR_BOOKMARKS:
445 vlc_mutex_lock( &p_input->p->input.p_item->lock );
446 if( p_input->p->i_bookmark )
450 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
452 p_bkmk = p_input->p->bookmark[i];
453 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
455 vlc_seekpoint_Delete( p_bkmk );
457 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
459 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
461 UpdateBookmarksOption( p_input );
465 case INPUT_SET_BOOKMARK:
466 i_bkmk = (int)va_arg( args, int );
468 vlc_mutex_lock( &p_input->p->input.p_item->lock );
469 if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
474 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
476 pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
477 i_ret = var_Set( p_input, "time", pos );
479 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
481 // don't crash on bookmarks in live streams
482 if( stream_Size( p_input->p->input.p_stream ) == 0 )
484 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
487 pos.f_float = !p_input->p->input.p_stream ? 0 :
488 p_input->p->bookmark[i_bkmk]->i_byte_offset /
489 stream_Size( p_input->p->input.p_stream );
490 i_ret = var_Set( p_input, "position", pos );
495 i_ret = var_Set( p_input, "position", pos );
498 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
503 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
509 case INPUT_ADD_OPTION:
511 const char *psz_option = va_arg( args, const char * );
512 const char *psz_value = va_arg( args, const char * );
516 if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
519 i = input_item_AddOpt( p_input->p->input.p_item, str,
520 VLC_INPUT_OPTION_UNIQUE );
525 case INPUT_GET_BYTE_POSITION:
526 pi_64 = (int64_t*)va_arg( args, int64_t * );
527 *pi_64 = !p_input->p->input.p_stream ? 0 :
528 stream_Tell( p_input->p->input.p_stream );
531 case INPUT_SET_BYTE_SIZE:
532 pi_64 = (int64_t*)va_arg( args, int64_t * );
533 *pi_64 = !p_input->p->input.p_stream ? 0 :
534 stream_Size( p_input->p->input.p_stream );
537 case INPUT_GET_VIDEO_FPS:
540 pf = (double*)va_arg( args, double * );
541 vlc_mutex_lock( &p_input->p->input.p_item->lock );
542 *pf = p_input->p->input.f_fps;
543 for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
544 *pf = p_input->p->slave[i]->f_fps;
545 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
549 case INPUT_ADD_SLAVE:
550 psz = (char*)va_arg( args, char * );
553 val.psz_string = strdup( psz );
554 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
558 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
560 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
561 int *pi_attachment = (int*)va_arg( args, int * );
564 vlc_mutex_lock( &p_input->p->input.p_item->lock );
565 if( p_input->p->i_attachment <= 0 )
567 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
568 *ppp_attachment = NULL;
572 *pi_attachment = p_input->p->i_attachment;
573 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
574 for( i = 0; i < p_input->p->i_attachment; i++ )
575 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
577 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
581 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
583 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
584 const char *psz_name = (const char*)va_arg( args, const char * );
587 vlc_mutex_lock( &p_input->p->input.p_item->lock );
588 for( i = 0; i < p_input->p->i_attachment; i++ )
590 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
592 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
593 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
597 *pp_attachment = NULL;
598 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
602 case INPUT_SET_RECORD_STATE:
603 b_bool = (bool)va_arg( args, int );
604 var_SetBool( p_input, "record", b_bool );
607 case INPUT_GET_RECORD_STATE:
608 pb_bool = (bool*)va_arg( args, bool* );
609 *pb_bool = var_GetBool( p_input, "record" );
613 msg_Err( p_input, "unknown query in input_vaControl" );
618 static void UpdateBookmarksOption( input_thread_t *p_input )
621 char *psz_value = NULL, *psz_next = NULL;
623 vlc_mutex_lock( &p_input->p->input.p_item->lock );
624 if( p_input->p->i_bookmark > 0 )
626 for( i = 0; i < p_input->p->i_bookmark; i++ )
628 i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
629 p_input->p->bookmark[i]->psz_name,
630 p_input->p->bookmark[i]->i_byte_offset,
631 p_input->p->bookmark[i]->i_time_offset/1000000 );
633 psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
635 for( i = 0; i < p_input->p->i_bookmark; i++ )
637 sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
638 p_input->p->bookmark[i]->psz_name,
639 p_input->p->bookmark[i]->i_byte_offset,
640 p_input->p->bookmark[i]->i_time_offset/1000000 );
642 psz_next += strlen( psz_next );
643 if( i < p_input->p->i_bookmark - 1)
644 *psz_next = ','; psz_next++;
647 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
649 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
650 psz_value ? psz_value : "" );