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 *****************************************************************************/
29 #include "input_internal.h"
30 #include "vlc_playlist.h"
33 static void UpdateBookmarksOption( input_thread_t * );
34 static void NotifyPlaylist( input_thread_t * );
36 /****************************************************************************
38 ****************************************************************************/
40 * Control function for inputs.
41 * \param p_input input handle
42 * \param i_query query type
43 * \return VLC_SUCCESS if ok
45 int input_Control( input_thread_t *p_input, int i_query, ... )
50 va_start( args, i_query );
51 i_result = input_vaControl( p_input, i_query, args );
57 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
59 seekpoint_t *p_bkmk, ***ppp_bkmk;
72 case INPUT_GET_POSITION:
73 pf = (double*)va_arg( args, double * );
74 *pf = var_GetFloat( p_input, "position" );
77 case INPUT_SET_POSITION:
78 f = (double)va_arg( args, double );
79 return var_SetFloat( p_input, "position", f );
81 case INPUT_GET_LENGTH:
82 pi_64 = (int64_t*)va_arg( args, int64_t * );
83 *pi_64 = var_GetTime( p_input, "length" );
87 pi_64 = (int64_t*)va_arg( args, int64_t * );
88 *pi_64 = var_GetTime( p_input, "time" );
92 i_64 = (int64_t)va_arg( args, int64_t );
93 return var_SetTime( p_input, "time", i_64 );
96 pi_int = (int*)va_arg( args, int * );
97 *pi_int = var_GetInteger( p_input, "rate" );
101 i_int = (int)va_arg( args, int );
102 return var_SetInteger( p_input, "rate", i_int );
104 case INPUT_GET_STATE:
105 pi_int = (int*)va_arg( args, int * );
106 *pi_int = var_GetInteger( p_input, "state" );
109 case INPUT_SET_STATE:
110 i_int = (int)va_arg( args, int );
111 return var_SetInteger( p_input, "state", i_int );
113 case INPUT_GET_AUDIO_DELAY:
114 pi_64 = (int64_t*)va_arg( args, int64_t * );
115 *pi_64 = var_GetTime( p_input, "audio-delay" );
118 case INPUT_GET_SPU_DELAY:
119 pi_64 = (int64_t*)va_arg( args, int64_t * );
120 *pi_64 = var_GetTime( p_input, "spu-delay" );
123 case INPUT_SET_AUDIO_DELAY:
124 i_64 = (int64_t)va_arg( args, int64_t );
125 return var_SetTime( p_input, "audio-delay", i_64 );
127 case INPUT_SET_SPU_DELAY:
128 i_64 = (int64_t)va_arg( args, int64_t );
129 return var_SetTime( p_input, "spu-delay", i_64 );
133 /* FIXME : Impossible to use input_ItemAddInfo because of
134 * the ... problem ? */
135 char *psz_cat = (char *)va_arg( args, char * );
136 char *psz_name = (char *)va_arg( args, char * );
137 char *psz_format = (char *)va_arg( args, char * );
139 info_category_t *p_cat;
143 vlc_mutex_lock( &p_input->p->input.p_item->lock );
144 for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
146 if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
150 if( i == p_input->p->input.p_item->i_categories )
152 p_cat = malloc( sizeof( info_category_t ) );
155 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
158 p_cat->psz_name = strdup( psz_cat );
160 p_cat->pp_infos = NULL;
161 INSERT_ELEM( p_input->p->input.p_item->pp_categories,
162 p_input->p->input.p_item->i_categories,
163 p_input->p->input.p_item->i_categories, p_cat );
166 p_cat = p_input->p->input.p_item->pp_categories[i];
168 for( i = 0; i < p_cat->i_infos; i++ )
170 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
172 if( p_cat->pp_infos[i]->psz_value )
173 free( p_cat->pp_infos[i]->psz_value );
178 if( i == p_cat->i_infos )
180 p_info = malloc( sizeof( info_t ) );
183 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
187 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
188 p_cat->i_infos, p_info );
189 p_info->psz_name = strdup( psz_name );
192 p_info = p_cat->pp_infos[i];
193 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
194 p_info->psz_value = NULL;
196 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
198 if( !p_input->b_preparsing )
199 NotifyPlaylist( p_input );
205 char *psz_cat = (char *)va_arg( args, char * );
206 char *psz_name = (char *)va_arg( args, char * );
208 info_category_t *p_cat = NULL;
212 vlc_mutex_lock( &p_input->p->input.p_item->lock );
213 for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
215 if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
218 p_cat = p_input->p->input.p_item->pp_categories[i_cat];
224 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
230 /* Remove a specific info */
231 for( i = 0; i < p_cat->i_infos; i++ )
233 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
235 free( p_cat->pp_infos[i]->psz_name );
236 if( p_cat->pp_infos[i]->psz_value )
237 free( p_cat->pp_infos[i]->psz_value );
238 free( p_cat->pp_infos[i] );
239 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
243 if( i >= p_cat->i_infos )
245 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
251 /* Remove the complete categorie */
252 for( i = 0; i < p_cat->i_infos; i++ )
254 free( p_cat->pp_infos[i]->psz_name );
255 if( p_cat->pp_infos[i]->psz_value )
256 free( p_cat->pp_infos[i]->psz_value );
257 free( p_cat->pp_infos[i] );
259 if( p_cat->pp_infos )
260 free( p_cat->pp_infos );
261 REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
263 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
265 if( !p_input->b_preparsing )
266 NotifyPlaylist( p_input );
274 char *psz_cat = (char *)va_arg( args, char * );
275 char *psz_name = (char *)va_arg( args, char * );
276 char **ppsz_value = (char **)va_arg( args, char ** );
277 int i_ret = VLC_EGENERIC;
280 *ppsz_value = input_ItemGetInfo( p_input->p->input.p_item,
287 char *psz_name = (char *)va_arg( args, char * );
289 if( !psz_name ) return VLC_EGENERIC;
291 vlc_mutex_lock( &p_input->p->input.p_item->lock );
292 if( p_input->p->input.p_item->psz_name )
293 free( p_input->p->input.p_item->psz_name );
294 p_input->p->input.p_item->psz_name = strdup( psz_name );
295 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
297 if( !p_input->b_preparsing )
298 NotifyPlaylist( p_input );
303 case INPUT_ADD_BOOKMARK:
304 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
305 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
307 vlc_mutex_lock( &p_input->p->input.p_item->lock );
308 if( !p_bkmk->psz_name )
310 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
311 p_input->p->i_bookmark ) == -1 )
312 p_bkmk->psz_name = NULL;
315 TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
317 /* Reflect the changes on the object var */
318 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
320 vlc_value_t val, text;
323 for( i = 0; i < p_input->p->i_bookmark; i++ )
326 text.psz_string = p_input->p->bookmark[i]->psz_name;
327 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
331 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
333 UpdateBookmarksOption( p_input );
337 case INPUT_CHANGE_BOOKMARK:
338 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
339 i_bkmk = (int)va_arg( args, int );
341 vlc_mutex_lock( &p_input->p->input.p_item->lock );
342 if( i_bkmk < p_input->p->i_bookmark )
344 vlc_value_t val, text;
347 p_input->p->bookmark[i_bkmk] = p_bkmk;
349 /* Reflect the changes on the object var */
350 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
351 for( i = 0; i < p_input->p->i_bookmark; i++ )
354 text.psz_string = p_input->p->bookmark[i]->psz_name;
355 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
359 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
361 UpdateBookmarksOption( p_input );
365 case INPUT_DEL_BOOKMARK:
366 i_bkmk = (int)va_arg( args, int );
368 vlc_mutex_lock( &p_input->p->input.p_item->lock );
369 if( i_bkmk < p_input->p->i_bookmark )
371 vlc_value_t val, text;
374 p_bkmk = p_input->p->bookmark[i_bkmk];
375 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
377 vlc_seekpoint_Delete( p_bkmk );
379 /* Reflect the changes on the object var */
380 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
381 for( i = 0; i < p_input->p->i_bookmark; i++ )
384 text.psz_string = p_input->p->bookmark[i]->psz_name;
385 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
388 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
390 UpdateBookmarksOption( p_input );
394 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
398 case INPUT_GET_BOOKMARKS:
399 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
400 pi_bkmk = (int *)va_arg( args, int * );
402 vlc_mutex_lock( &p_input->p->input.p_item->lock );
403 if( p_input->p->i_bookmark )
407 *pi_bkmk = p_input->p->i_bookmark;
408 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
409 p_input->p->i_bookmark );
410 for( i = 0; i < p_input->p->i_bookmark; i++ )
413 vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
416 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
424 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
429 case INPUT_CLEAR_BOOKMARKS:
431 vlc_mutex_lock( &p_input->p->input.p_item->lock );
432 if( p_input->p->i_bookmark )
436 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
438 p_bkmk = p_input->p->bookmark[i];
439 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
441 vlc_seekpoint_Delete( p_bkmk );
443 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
445 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
447 UpdateBookmarksOption( p_input );
451 case INPUT_SET_BOOKMARK:
452 i_bkmk = (int)va_arg( args, int );
454 vlc_mutex_lock( &p_input->p->input.p_item->lock );
455 if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
460 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
462 pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
463 i_ret = var_Set( p_input, "time", pos );
465 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
467 // don't crash on bookmarks in live streams
468 if( stream_Size( p_input->p->input.p_stream ) == 0 )
470 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
473 pos.f_float = !p_input->p->input.p_stream ? 0 :
474 p_input->p->bookmark[i_bkmk]->i_byte_offset /
475 stream_Size( p_input->p->input.p_stream );
476 i_ret = var_Set( p_input, "position", pos );
481 i_ret = var_Set( p_input, "position", pos );
484 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
489 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
495 case INPUT_ADD_OPTION:
497 char *psz_option = (char *)va_arg( args, char * );
498 char *psz_value = (char *)va_arg( args, char * );
501 vlc_mutex_lock( &p_input->p->input.p_item->lock );
502 /* Check if option already exists */
503 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
505 if( !strncmp( p_input->p->input.p_item->ppsz_options[i],
506 psz_option, strlen( psz_option ) ) &&
507 p_input->p->input.p_item->ppsz_options[i][strlen(psz_option)]
510 free( p_input->p->input.p_item->ppsz_options[i] );
514 if( i == p_input->p->input.p_item->i_options )
516 p_input->p->input.p_item->i_options++;
517 p_input->p->input.p_item->ppsz_options =
518 realloc( p_input->p->input.p_item->ppsz_options,
519 p_input->p->input.p_item->i_options *
523 if( asprintf( &p_input->p->input.p_item->ppsz_options[i],
524 "%s=%s", psz_option, psz_value ) == -1 )
525 p_input->p->input.p_item->ppsz_options[i] = NULL;
526 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
531 case INPUT_GET_BYTE_POSITION:
532 pi_64 = (int64_t*)va_arg( args, int64_t * );
533 *pi_64 = !p_input->p->input.p_stream ? 0 :
534 stream_Tell( p_input->p->input.p_stream );
537 case INPUT_SET_BYTE_SIZE:
538 pi_64 = (int64_t*)va_arg( args, int64_t * );
539 *pi_64 = !p_input->p->input.p_stream ? 0 :
540 stream_Size( p_input->p->input.p_stream );
543 case INPUT_GET_VIDEO_FPS:
546 pf = (double*)va_arg( args, double * );
547 vlc_mutex_lock( &p_input->p->input.p_item->lock );
548 *pf = p_input->p->input.f_fps;
549 for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
550 *pf = p_input->p->slave[i]->f_fps;
551 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
555 case INPUT_ADD_SLAVE:
556 psz = (char*)va_arg( args, char * );
559 val.psz_string = strdup( psz );
560 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
564 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
566 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
567 int *pi_attachment = (int*)va_arg( args, int * );
570 vlc_mutex_lock( &p_input->p->input.p_item->lock );
571 if( p_input->p->i_attachment <= 0 )
573 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
574 *ppp_attachment = NULL;
578 *pi_attachment = p_input->p->i_attachment;
579 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
580 for( i = 0; i < p_input->p->i_attachment; i++ )
581 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
583 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
587 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
589 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
590 const char *psz_name = (const char*)va_arg( args, const char * );
593 vlc_mutex_lock( &p_input->p->input.p_item->lock );
594 for( i = 0; i < p_input->p->i_attachment; i++ )
596 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
598 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
599 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
603 *pp_attachment = NULL;
604 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
610 msg_Err( p_input, "unknown query in input_vaControl" );
615 static void NotifyPlaylist( input_thread_t *p_input )
617 playlist_t *p_playlist = pl_Yield( p_input );
618 var_SetInteger( p_playlist, "item-change",
619 p_input->p->input.p_item->i_id );
620 pl_Release( p_input );
623 static void UpdateBookmarksOption( input_thread_t *p_input )
626 char *psz_value = NULL, *psz_next = NULL;
628 vlc_mutex_lock( &p_input->p->input.p_item->lock );
629 for( i = 0; i < p_input->p->i_bookmark; i++ )
631 i_len += snprintf( NULL, 0, "{name=%s,bytes="I64Fd",time="I64Fd"}",
632 p_input->p->bookmark[i]->psz_name,
633 p_input->p->bookmark[i]->i_byte_offset,
634 p_input->p->bookmark[i]->i_time_offset/1000000 );
636 for( i = 0; i < p_input->p->i_bookmark; i++ )
638 if( !i ) psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
640 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
641 p_input->p->bookmark[i]->psz_name,
642 p_input->p->bookmark[i]->i_byte_offset,
643 p_input->p->bookmark[i]->i_time_offset/1000000 );
645 psz_next += strlen( psz_next );
646 if( i < p_input->p->i_bookmark - 1)
648 *psz_next = ','; psz_next++;
651 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
653 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
654 psz_value ? psz_value : "" );