LGPL
[vlc.git] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <assert.h>
33
34 #include "input_internal.h"
35 #include "event.h"
36 #include "resource.h"
37 #include "es_out.h"
38
39
40 static void UpdateBookmarksOption( input_thread_t * );
41
42 /****************************************************************************
43  * input_Control
44  ****************************************************************************/
45 /**
46  * Control function for inputs.
47  * \param p_input input handle
48  * \param i_query query type
49  * \return VLC_SUCCESS if ok
50  */
51 int input_Control( input_thread_t *p_input, int i_query, ...  )
52 {
53     va_list args;
54     int     i_result;
55
56     va_start( args, i_query );
57     i_result = input_vaControl( p_input, i_query, args );
58     va_end( args );
59
60     return i_result;
61 }
62
63 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
64 {
65     seekpoint_t *p_bkmk, ***ppp_bkmk;
66     int i_bkmk = 0;
67     int *pi_bkmk;
68
69     int i_int, *pi_int;
70     bool b_bool, *pb_bool;
71     double f, *pf;
72     int64_t i_64, *pi_64;
73
74     char *psz;
75     vlc_value_t val;
76
77     switch( i_query )
78     {
79         case INPUT_GET_POSITION:
80             pf = (double*)va_arg( args, double * );
81             *pf = var_GetFloat( p_input, "position" );
82             return VLC_SUCCESS;
83
84         case INPUT_SET_POSITION:
85             f = (double)va_arg( args, double );
86             return var_SetFloat( p_input, "position", f );
87
88         case INPUT_GET_LENGTH:
89             pi_64 = (int64_t*)va_arg( args, int64_t * );
90             *pi_64 = var_GetTime( p_input, "length" );
91             return VLC_SUCCESS;
92
93         case INPUT_GET_TIME:
94             pi_64 = (int64_t*)va_arg( args, int64_t * );
95             *pi_64 = var_GetTime( p_input, "time" );
96             return VLC_SUCCESS;
97
98         case INPUT_SET_TIME:
99             i_64 = (int64_t)va_arg( args, int64_t );
100             return var_SetTime( p_input, "time", i_64 );
101
102         case INPUT_GET_RATE:
103             pi_int = (int*)va_arg( args, int * );
104             *pi_int = INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" );
105             return VLC_SUCCESS;
106
107         case INPUT_SET_RATE:
108             i_int = (int)va_arg( args, int );
109             return var_SetFloat( p_input, "rate",
110                                  (float)INPUT_RATE_DEFAULT / (float)i_int );
111
112         case INPUT_GET_STATE:
113             pi_int = (int*)va_arg( args, int * );
114             *pi_int = var_GetInteger( p_input, "state" );
115             return VLC_SUCCESS;
116
117         case INPUT_SET_STATE:
118             i_int = (int)va_arg( args, int );
119             return var_SetInteger( p_input, "state", i_int );
120
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" );
124             return VLC_SUCCESS;
125
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" );
129             return VLC_SUCCESS;
130
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 );
134
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 );
138
139         case INPUT_ADD_INFO:
140         {
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 * );
144
145             char *psz_value;
146
147             if( vasprintf( &psz_value, psz_format, args ) == -1 )
148                 return VLC_EGENERIC;
149
150             int i_ret = input_item_AddInfo( p_input->p->p_item,
151                                             psz_cat, psz_name, "%s", psz_value );
152             free( psz_value );
153
154             if( !p_input->b_preparsing && !i_ret )
155                 input_SendEventMetaInfo( p_input );
156             return i_ret;
157         }
158         case INPUT_REPLACE_INFOS:
159         case INPUT_MERGE_INFOS:
160         {
161             info_category_t *p_cat = va_arg( args, info_category_t * );
162
163             if( i_query == INPUT_REPLACE_INFOS )
164                 input_item_ReplaceInfos( p_input->p->p_item, p_cat );
165             else
166                 input_item_MergeInfos( p_input->p->p_item, p_cat );
167
168             if( !p_input->b_preparsing )
169                 input_SendEventMetaInfo( p_input );
170             return VLC_SUCCESS;
171         }
172         case INPUT_DEL_INFO:
173         {
174             char *psz_cat = (char *)va_arg( args, char * );
175             char *psz_name = (char *)va_arg( args, char * );
176
177             int i_ret = input_item_DelInfo( p_input->p->p_item,
178                                             psz_cat, psz_name );
179
180             if( !p_input->b_preparsing && !i_ret )
181                 input_SendEventMetaInfo( p_input );
182             return i_ret;
183         }
184         case INPUT_GET_INFO:
185         {
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;
190             *ppsz_value = NULL;
191
192             *ppsz_value = input_item_GetInfo( p_input->p->p_item,
193                                                   psz_cat, psz_name );
194             return i_ret;
195         }
196
197         case INPUT_SET_NAME:
198         {
199             char *psz_name = (char *)va_arg( args, char * );
200
201             if( !psz_name ) return VLC_EGENERIC;
202
203             input_item_SetName( p_input->p->p_item, psz_name );
204
205             if( !p_input->b_preparsing )
206                 input_SendEventMetaName( p_input, psz_name );
207             return VLC_SUCCESS;
208         }
209
210         case INPUT_ADD_BOOKMARK:
211             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
212             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
213
214             vlc_mutex_lock( &p_input->p->p_item->lock );
215             if( !p_bkmk->psz_name )
216             {
217                  if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
218                                p_input->p->i_bookmark ) == -1 )
219                      p_bkmk->psz_name = NULL;
220             }
221
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 );
224
225             UpdateBookmarksOption( p_input );
226
227             return VLC_SUCCESS;
228
229         case INPUT_CHANGE_BOOKMARK:
230             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
231             i_bkmk = (int)va_arg( args, int );
232
233             vlc_mutex_lock( &p_input->p->p_item->lock );
234             if( i_bkmk < p_input->p->i_bookmark )
235             {
236                 vlc_seekpoint_Delete( p_input->p->pp_bookmark[i_bkmk] );
237                 p_input->p->pp_bookmark[i_bkmk] = vlc_seekpoint_Duplicate( p_bkmk );
238             }
239             vlc_mutex_unlock( &p_input->p->p_item->lock );
240
241             UpdateBookmarksOption( p_input );
242
243             return VLC_SUCCESS;
244
245         case INPUT_DEL_BOOKMARK:
246             i_bkmk = (int)va_arg( args, int );
247
248             vlc_mutex_lock( &p_input->p->p_item->lock );
249             if( i_bkmk < p_input->p->i_bookmark )
250             {
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 );
254
255                 vlc_mutex_unlock( &p_input->p->p_item->lock );
256
257                 UpdateBookmarksOption( p_input );
258
259                 return VLC_SUCCESS;
260             }
261             vlc_mutex_unlock( &p_input->p->p_item->lock );
262
263             return VLC_EGENERIC;
264
265         case INPUT_GET_BOOKMARKS:
266             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
267             pi_bkmk = (int *)va_arg( args, int * );
268
269             vlc_mutex_lock( &p_input->p->p_item->lock );
270             if( p_input->p->i_bookmark )
271             {
272                 int i;
273
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++ )
278                 {
279                     (*ppp_bkmk)[i] =
280                         vlc_seekpoint_Duplicate( p_input->p->pp_bookmark[i] );
281                 }
282
283                 vlc_mutex_unlock( &p_input->p->p_item->lock );
284                 return VLC_SUCCESS;
285             }
286             else
287             {
288                 *ppp_bkmk = NULL;
289                 *pi_bkmk = 0;
290
291                 vlc_mutex_unlock( &p_input->p->p_item->lock );
292                 return VLC_EGENERIC;
293             }
294             break;
295
296         case INPUT_CLEAR_BOOKMARKS:
297
298             vlc_mutex_lock( &p_input->p->p_item->lock );
299             while( p_input->p->i_bookmark > 0 )
300             {
301                 p_bkmk = p_input->p->pp_bookmark[p_input->p->i_bookmark-1];
302
303                 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark,
304                             p_bkmk );
305                 vlc_seekpoint_Delete( p_bkmk );
306             }
307             vlc_mutex_unlock( &p_input->p->p_item->lock );
308
309             UpdateBookmarksOption( p_input );
310             return VLC_SUCCESS;
311
312         case INPUT_SET_BOOKMARK:
313             i_bkmk = (int)va_arg( args, int );
314
315             val.i_int = i_bkmk;
316             input_ControlPush( p_input, INPUT_CONTROL_SET_BOOKMARK, &val );
317
318             return VLC_SUCCESS;
319
320         case INPUT_GET_BOOKMARK:
321             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
322
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 );
326             return VLC_SUCCESS;
327
328         case INPUT_GET_TITLE_INFO:
329         {
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 * );
332
333             vlc_mutex_lock( &p_input->p->p_item->lock );
334
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;
338
339             if( p_input->p->i_title && p_input->p->i_title > *pi_req_title_offset )
340             {
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 );
343                 return VLC_SUCCESS;
344             }
345             else
346             {
347                 vlc_mutex_unlock( &p_input->p->p_item->lock );
348                 return VLC_EGENERIC;
349             }
350         }
351
352         case INPUT_GET_VIDEO_FPS:
353             pf = (double*)va_arg( args, double * );
354
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 );
358             return VLC_SUCCESS;
359
360         case INPUT_ADD_SLAVE:
361             psz = (char*)va_arg( args, char * );
362             if( psz && *psz )
363             {
364                 val.psz_string = strdup( psz );
365                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
366             }
367             return VLC_SUCCESS;
368
369         case INPUT_ADD_SUBTITLE:
370             psz = (char*)va_arg( args, char * );
371             b_bool = (bool)va_arg( args, int );
372
373             if( !psz || *psz == '\0' )
374                 return VLC_EGENERIC;
375             if( b_bool && !subtitles_Filter( psz ) )
376                 return VLC_EGENERIC;
377
378             val.psz_string = strdup( psz );
379             input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
380             return VLC_SUCCESS;
381
382         case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int*  res=can fail */
383         {
384             input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
385             int *pi_attachment = (int*)va_arg( args, int * );
386             int i;
387
388             vlc_mutex_lock( &p_input->p->p_item->lock );
389             if( p_input->p->i_attachment <= 0 )
390             {
391                 vlc_mutex_unlock( &p_input->p->p_item->lock );
392                 *ppp_attachment = NULL;
393                 *pi_attachment = 0;
394                 return VLC_EGENERIC;
395             }
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] );
400
401             vlc_mutex_unlock( &p_input->p->p_item->lock );
402             return VLC_SUCCESS;
403         }
404
405         case INPUT_GET_ATTACHMENT:  /* arg1=input_attachment_t**, arg2=char*  res=can fail */
406         {
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 * );
409             int i;
410
411             vlc_mutex_lock( &p_input->p->p_item->lock );
412             for( i = 0; i < p_input->p->i_attachment; i++ )
413             {
414                 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
415                 {
416                     *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
417                     vlc_mutex_unlock( &p_input->p->p_item->lock );
418                     return VLC_SUCCESS;
419                 }
420             }
421             *pp_attachment = NULL;
422             vlc_mutex_unlock( &p_input->p->p_item->lock );
423             return VLC_EGENERIC;
424         }
425
426         case INPUT_SET_RECORD_STATE:
427             b_bool = (bool)va_arg( args, int );
428             var_SetBool( p_input, "record", b_bool );
429             return VLC_SUCCESS;
430
431         case INPUT_GET_RECORD_STATE:
432             pb_bool = (bool*)va_arg( args, bool* );
433             *pb_bool = var_GetBool( p_input, "record" );
434             return VLC_SUCCESS;
435
436         case INPUT_RESTART_ES:
437             val.i_int = (int)va_arg( args, int );
438             input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
439             return VLC_SUCCESS;
440
441         case INPUT_GET_AOUT:
442         {
443             audio_output_t *p_aout = input_resource_HoldAout( p_input->p->p_resource );
444             if( !p_aout )
445                 return VLC_EGENERIC;
446
447             audio_output_t **pp_aout = (audio_output_t**)va_arg( args, audio_output_t** );
448             *pp_aout = p_aout;
449             return VLC_SUCCESS;
450         }
451
452         case INPUT_GET_VOUTS:
453         {
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 * );
456
457             input_resource_HoldVouts( p_input->p->p_resource, ppp_vout, pi_vout );
458             if( *pi_vout <= 0 )
459                 return VLC_EGENERIC;
460             return VLC_SUCCESS;
461         }
462
463         case INPUT_GET_ES_OBJECTS:
464         {
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 ** );
469
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 );
472         }
473
474         case INPUT_GET_PCR_SYSTEM:
475         {
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 );
479         }
480
481         case INPUT_MODIFY_PCR_SYSTEM:
482         {
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 );
486         }
487
488         default:
489             msg_Err( p_input, "unknown query in input_vaControl" );
490             return VLC_EGENERIC;
491     }
492 }
493
494 static void UpdateBookmarksOption( input_thread_t *p_input )
495 {
496     vlc_mutex_lock( &p_input->p->p_item->lock );
497
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++ )
501     {
502         vlc_value_t val, text;
503
504         val.i_int = i;
505         text.psz_string = p_input->p->pp_bookmark[i]->psz_name;
506         var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
507                     &val, &text );
508     }
509
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++ )
514     {
515         const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
516
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 );
521     }
522
523     char *psz_value = malloc( i_len + p_input->p->i_bookmark + 1 );
524     char *psz_next = psz_value;
525
526     psz_next += sprintf( psz_next, "bookmarks=" );
527     for( int i = 0; i < p_input->p->i_bookmark && psz_value != NULL; i++ )
528     {
529         const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
530
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 );
535
536         if( i < p_input->p->i_bookmark - 1)
537             *psz_next++ = ',';
538     }
539     vlc_mutex_unlock( &p_input->p->p_item->lock );
540
541     if( psz_value )
542         input_item_AddOption( p_input->p->p_item, psz_value, VLC_INPUT_OPTION_UNIQUE );
543
544     free( psz_value );
545
546     input_SendEventBookmark( p_input );
547 }
548