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 *****************************************************************************/
33 #include "input_internal.h"
34 #include "vlc_playlist.h"
37 static void UpdateBookmarksOption( input_thread_t * );
38 static void NotifyPlaylist( input_thread_t * );
40 /****************************************************************************
42 ****************************************************************************/
44 * Control function for inputs.
45 * \param p_input input handle
46 * \param i_query query type
47 * \return VLC_SUCCESS if ok
49 int input_Control( input_thread_t *p_input, int i_query, ... )
54 va_start( args, i_query );
55 i_result = input_vaControl( p_input, i_query, args );
61 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
63 seekpoint_t *p_bkmk, ***ppp_bkmk;
76 case INPUT_GET_POSITION:
77 pf = (double*)va_arg( args, double * );
78 *pf = var_GetFloat( p_input, "position" );
81 case INPUT_SET_POSITION:
82 f = (double)va_arg( args, double );
83 return var_SetFloat( p_input, "position", f );
85 case INPUT_GET_LENGTH:
86 pi_64 = (int64_t*)va_arg( args, int64_t * );
87 *pi_64 = var_GetTime( p_input, "length" );
91 pi_64 = (int64_t*)va_arg( args, int64_t * );
92 *pi_64 = var_GetTime( p_input, "time" );
96 i_64 = (int64_t)va_arg( args, int64_t );
97 return var_SetTime( p_input, "time", i_64 );
100 pi_int = (int*)va_arg( args, int * );
101 *pi_int = var_GetInteger( p_input, "rate" );
105 i_int = (int)va_arg( args, int );
106 return var_SetInteger( p_input, "rate", i_int );
108 case INPUT_GET_STATE:
109 pi_int = (int*)va_arg( args, int * );
110 *pi_int = var_GetInteger( p_input, "state" );
113 case INPUT_SET_STATE:
114 i_int = (int)va_arg( args, int );
115 return var_SetInteger( p_input, "state", i_int );
117 case INPUT_GET_AUDIO_DELAY:
118 pi_64 = (int64_t*)va_arg( args, int64_t * );
119 *pi_64 = var_GetTime( p_input, "audio-delay" );
122 case INPUT_GET_SPU_DELAY:
123 pi_64 = (int64_t*)va_arg( args, int64_t * );
124 *pi_64 = var_GetTime( p_input, "spu-delay" );
127 case INPUT_SET_AUDIO_DELAY:
128 i_64 = (int64_t)va_arg( args, int64_t );
129 return var_SetTime( p_input, "audio-delay", i_64 );
131 case INPUT_SET_SPU_DELAY:
132 i_64 = (int64_t)va_arg( args, int64_t );
133 return var_SetTime( p_input, "spu-delay", i_64 );
137 /* FIXME : Impossible to use input_ItemAddInfo because of
138 * the ... problem ? */
139 char *psz_cat = (char *)va_arg( args, char * );
140 char *psz_name = (char *)va_arg( args, char * );
141 char *psz_format = (char *)va_arg( args, char * );
143 info_category_t *p_cat;
147 vlc_mutex_lock( &p_input->p->input.p_item->lock );
148 for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
150 if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
154 if( i == p_input->p->input.p_item->i_categories )
156 p_cat = malloc( sizeof( info_category_t ) );
159 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
162 p_cat->psz_name = strdup( psz_cat );
164 p_cat->pp_infos = NULL;
165 INSERT_ELEM( p_input->p->input.p_item->pp_categories,
166 p_input->p->input.p_item->i_categories,
167 p_input->p->input.p_item->i_categories, p_cat );
170 p_cat = p_input->p->input.p_item->pp_categories[i];
172 for( i = 0; i < p_cat->i_infos; i++ )
174 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
176 if( p_cat->pp_infos[i]->psz_value )
177 free( p_cat->pp_infos[i]->psz_value );
182 if( i == p_cat->i_infos )
184 p_info = malloc( sizeof( info_t ) );
187 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
191 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
192 p_cat->i_infos, p_info );
193 p_info->psz_name = strdup( psz_name );
196 p_info = p_cat->pp_infos[i];
197 if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
198 p_info->psz_value = NULL;
200 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
202 if( !p_input->b_preparsing )
203 NotifyPlaylist( p_input );
209 char *psz_cat = (char *)va_arg( args, char * );
210 char *psz_name = (char *)va_arg( args, char * );
212 info_category_t *p_cat = NULL;
216 vlc_mutex_lock( &p_input->p->input.p_item->lock );
217 for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
219 if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
222 p_cat = p_input->p->input.p_item->pp_categories[i_cat];
228 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
234 /* Remove a specific info */
235 for( i = 0; i < p_cat->i_infos; i++ )
237 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
239 free( p_cat->pp_infos[i]->psz_name );
240 if( p_cat->pp_infos[i]->psz_value )
241 free( p_cat->pp_infos[i]->psz_value );
242 free( p_cat->pp_infos[i] );
243 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
247 if( i >= p_cat->i_infos )
249 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
255 /* Remove the complete categorie */
256 for( i = 0; i < p_cat->i_infos; i++ )
258 free( p_cat->pp_infos[i]->psz_name );
259 if( p_cat->pp_infos[i]->psz_value )
260 free( p_cat->pp_infos[i]->psz_value );
261 free( p_cat->pp_infos[i] );
263 if( p_cat->pp_infos )
264 free( p_cat->pp_infos );
265 REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
267 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
269 if( !p_input->b_preparsing )
270 NotifyPlaylist( p_input );
278 char *psz_cat = (char *)va_arg( args, char * );
279 char *psz_name = (char *)va_arg( args, char * );
280 char **ppsz_value = (char **)va_arg( args, char ** );
281 int i_ret = VLC_EGENERIC;
284 *ppsz_value = input_ItemGetInfo( p_input->p->input.p_item,
291 char *psz_name = (char *)va_arg( args, char * );
293 if( !psz_name ) return VLC_EGENERIC;
295 vlc_mutex_lock( &p_input->p->input.p_item->lock );
296 if( p_input->p->input.p_item->psz_name )
297 free( p_input->p->input.p_item->psz_name );
298 p_input->p->input.p_item->psz_name = strdup( psz_name );
299 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
301 if( !p_input->b_preparsing )
302 NotifyPlaylist( p_input );
307 case INPUT_ADD_BOOKMARK:
308 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
309 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
311 vlc_mutex_lock( &p_input->p->input.p_item->lock );
312 if( !p_bkmk->psz_name )
314 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
315 p_input->p->i_bookmark ) == -1 )
316 p_bkmk->psz_name = NULL;
319 TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
321 /* Reflect the changes on the object var */
322 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
324 vlc_value_t val, text;
327 for( i = 0; i < p_input->p->i_bookmark; i++ )
330 text.psz_string = p_input->p->bookmark[i]->psz_name;
331 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
335 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
337 UpdateBookmarksOption( p_input );
341 case INPUT_CHANGE_BOOKMARK:
342 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
343 i_bkmk = (int)va_arg( args, int );
345 vlc_mutex_lock( &p_input->p->input.p_item->lock );
346 if( i_bkmk < p_input->p->i_bookmark )
348 vlc_value_t val, text;
351 p_input->p->bookmark[i_bkmk] = p_bkmk;
353 /* Reflect the changes on the object var */
354 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
355 for( i = 0; i < p_input->p->i_bookmark; i++ )
358 text.psz_string = p_input->p->bookmark[i]->psz_name;
359 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
363 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
365 UpdateBookmarksOption( p_input );
369 case INPUT_DEL_BOOKMARK:
370 i_bkmk = (int)va_arg( args, int );
372 vlc_mutex_lock( &p_input->p->input.p_item->lock );
373 if( i_bkmk < p_input->p->i_bookmark )
375 vlc_value_t val, text;
378 p_bkmk = p_input->p->bookmark[i_bkmk];
379 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
381 vlc_seekpoint_Delete( p_bkmk );
383 /* Reflect the changes on the object var */
384 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
385 for( i = 0; i < p_input->p->i_bookmark; i++ )
388 text.psz_string = p_input->p->bookmark[i]->psz_name;
389 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
392 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
394 UpdateBookmarksOption( p_input );
398 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
402 case INPUT_GET_BOOKMARKS:
403 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
404 pi_bkmk = (int *)va_arg( args, int * );
406 vlc_mutex_lock( &p_input->p->input.p_item->lock );
407 if( p_input->p->i_bookmark )
411 *pi_bkmk = p_input->p->i_bookmark;
412 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
413 p_input->p->i_bookmark );
414 for( i = 0; i < p_input->p->i_bookmark; i++ )
417 vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
420 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
428 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
433 case INPUT_CLEAR_BOOKMARKS:
435 vlc_mutex_lock( &p_input->p->input.p_item->lock );
436 if( p_input->p->i_bookmark )
440 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
442 p_bkmk = p_input->p->bookmark[i];
443 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
445 vlc_seekpoint_Delete( p_bkmk );
447 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
449 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
451 UpdateBookmarksOption( p_input );
455 case INPUT_SET_BOOKMARK:
456 i_bkmk = (int)va_arg( args, int );
458 vlc_mutex_lock( &p_input->p->input.p_item->lock );
459 if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
464 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
466 pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
467 i_ret = var_Set( p_input, "time", pos );
469 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
471 // don't crash on bookmarks in live streams
472 if( stream_Size( p_input->p->input.p_stream ) == 0 )
474 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
477 pos.f_float = !p_input->p->input.p_stream ? 0 :
478 p_input->p->bookmark[i_bkmk]->i_byte_offset /
479 stream_Size( p_input->p->input.p_stream );
480 i_ret = var_Set( p_input, "position", pos );
485 i_ret = var_Set( p_input, "position", pos );
488 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
493 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
499 case INPUT_ADD_OPTION:
501 const char *psz_option = va_arg( args, const char * );
502 const char *psz_value = va_arg( args, const char * );
506 if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
509 i = input_ItemAddOpt( p_input->p->input.p_item, str,
510 VLC_INPUT_OPTION_UNIQUE );
515 case INPUT_GET_BYTE_POSITION:
516 pi_64 = (int64_t*)va_arg( args, int64_t * );
517 *pi_64 = !p_input->p->input.p_stream ? 0 :
518 stream_Tell( p_input->p->input.p_stream );
521 case INPUT_SET_BYTE_SIZE:
522 pi_64 = (int64_t*)va_arg( args, int64_t * );
523 *pi_64 = !p_input->p->input.p_stream ? 0 :
524 stream_Size( p_input->p->input.p_stream );
527 case INPUT_GET_VIDEO_FPS:
530 pf = (double*)va_arg( args, double * );
531 vlc_mutex_lock( &p_input->p->input.p_item->lock );
532 *pf = p_input->p->input.f_fps;
533 for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
534 *pf = p_input->p->slave[i]->f_fps;
535 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
539 case INPUT_ADD_SLAVE:
540 psz = (char*)va_arg( args, char * );
543 val.psz_string = strdup( psz );
544 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
548 case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int* res=can fail */
550 input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
551 int *pi_attachment = (int*)va_arg( args, int * );
554 vlc_mutex_lock( &p_input->p->input.p_item->lock );
555 if( p_input->p->i_attachment <= 0 )
557 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
558 *ppp_attachment = NULL;
562 *pi_attachment = p_input->p->i_attachment;
563 *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
564 for( i = 0; i < p_input->p->i_attachment; i++ )
565 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
567 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
571 case INPUT_GET_ATTACHMENT: /* arg1=input_attachment_t**, arg2=char* res=can fail */
573 input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
574 const char *psz_name = (const char*)va_arg( args, const char * );
577 vlc_mutex_lock( &p_input->p->input.p_item->lock );
578 for( i = 0; i < p_input->p->i_attachment; i++ )
580 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
582 *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
583 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
587 *pp_attachment = NULL;
588 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
594 msg_Err( p_input, "unknown query in input_vaControl" );
599 static void NotifyPlaylist( input_thread_t *p_input )
601 /* FIXME: We need to avoid that dependency on the playlist
602 * because it is a circular dependency:
603 * ( playlist -> input -> playlist ) */
604 playlist_t *p_playlist = vlc_object_find( p_input,
605 VLC_OBJECT_PLAYLIST, FIND_PARENT );
608 var_SetInteger( p_playlist, "item-change",
609 p_input->p->input.p_item->i_id );
610 vlc_object_release( p_playlist );
613 static void UpdateBookmarksOption( input_thread_t *p_input )
616 char *psz_value = NULL, *psz_next = NULL;
618 vlc_mutex_lock( &p_input->p->input.p_item->lock );
619 for( i = 0; i < p_input->p->i_bookmark; i++ )
621 i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
622 p_input->p->bookmark[i]->psz_name,
623 p_input->p->bookmark[i]->i_byte_offset,
624 p_input->p->bookmark[i]->i_time_offset/1000000 );
626 for( i = 0; i < p_input->p->i_bookmark; i++ )
628 if( !i ) psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
630 sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
631 p_input->p->bookmark[i]->psz_name,
632 p_input->p->bookmark[i]->i_byte_offset,
633 p_input->p->bookmark[i]->i_time_offset/1000000 );
635 psz_next += strlen( psz_next );
636 if( i < p_input->p->i_bookmark - 1)
638 *psz_next = ','; psz_next++;
641 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
643 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
644 psz_value ? psz_value : "" );