]> git.sesse.net Git - vlc/blob - src/input/control.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
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
33 #include "input_internal.h"
34 #include "event.h"
35 #include "resource.h"
36
37
38 static void UpdateBookmarksOption( input_thread_t * );
39
40 /****************************************************************************
41  * input_Control
42  ****************************************************************************/
43 /**
44  * Control function for inputs.
45  * \param p_input input handle
46  * \param i_query query type
47  * \return VLC_SUCCESS if ok
48  */
49 int input_Control( input_thread_t *p_input, int i_query, ...  )
50 {
51     va_list args;
52     int     i_result;
53
54     va_start( args, i_query );
55     i_result = input_vaControl( p_input, i_query, args );
56     va_end( args );
57
58     return i_result;
59 }
60
61 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
62 {
63     seekpoint_t *p_bkmk, ***ppp_bkmk;
64     int i_bkmk = 0;
65     int *pi_bkmk;
66
67     int i_int, *pi_int;
68     bool b_bool, *pb_bool;
69     double f, *pf;
70     int64_t i_64, *pi_64;
71
72     char *psz;
73     vlc_value_t val;
74
75     switch( i_query )
76     {
77         case INPUT_GET_POSITION:
78             pf = (double*)va_arg( args, double * );
79             *pf = var_GetFloat( p_input, "position" );
80             return VLC_SUCCESS;
81
82         case INPUT_SET_POSITION:
83             f = (double)va_arg( args, double );
84             return var_SetFloat( p_input, "position", f );
85
86         case INPUT_GET_LENGTH:
87             pi_64 = (int64_t*)va_arg( args, int64_t * );
88             *pi_64 = var_GetTime( p_input, "length" );
89             return VLC_SUCCESS;
90
91         case INPUT_GET_TIME:
92             pi_64 = (int64_t*)va_arg( args, int64_t * );
93             *pi_64 = var_GetTime( p_input, "time" );
94             return VLC_SUCCESS;
95
96         case INPUT_SET_TIME:
97             i_64 = (int64_t)va_arg( args, int64_t );
98             return var_SetTime( p_input, "time", i_64 );
99
100         case INPUT_GET_RATE:
101             pi_int = (int*)va_arg( args, int * );
102             *pi_int = var_GetInteger( p_input, "rate" );
103             return VLC_SUCCESS;
104
105         case INPUT_SET_RATE:
106             i_int = (int)va_arg( args, int );
107             return var_SetInteger( p_input, "rate", i_int );
108
109         case INPUT_GET_STATE:
110             pi_int = (int*)va_arg( args, int * );
111             *pi_int = var_GetInteger( p_input, "state" );
112             return VLC_SUCCESS;
113
114         case INPUT_SET_STATE:
115             i_int = (int)va_arg( args, int );
116             return var_SetInteger( p_input, "state", i_int );
117
118         case INPUT_GET_AUDIO_DELAY:
119             pi_64 = (int64_t*)va_arg( args, int64_t * );
120             *pi_64 = var_GetTime( p_input, "audio-delay" );
121             return VLC_SUCCESS;
122
123         case INPUT_GET_SPU_DELAY:
124             pi_64 = (int64_t*)va_arg( args, int64_t * );
125             *pi_64 = var_GetTime( p_input, "spu-delay" );
126             return VLC_SUCCESS;
127
128         case INPUT_SET_AUDIO_DELAY:
129             i_64 = (int64_t)va_arg( args, int64_t );
130             return var_SetTime( p_input, "audio-delay", i_64 );
131
132         case INPUT_SET_SPU_DELAY:
133             i_64 = (int64_t)va_arg( args, int64_t );
134             return var_SetTime( p_input, "spu-delay", i_64 );
135
136         case INPUT_ADD_INFO:
137         {
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 * );
141
142             char *psz_value;
143             
144             if( vasprintf( &psz_value, psz_format, args ) == -1 )
145                 return VLC_EGENERIC;
146
147             int i_ret = input_item_AddInfo( p_input->p->p_item,
148                                             psz_cat, psz_name, "%s", psz_value );
149             free( psz_value );
150
151             if( !p_input->b_preparsing && !i_ret )
152                 input_SendEventMetaInfo( p_input );
153             return i_ret;
154         }
155         case INPUT_DEL_INFO:
156         {
157             char *psz_cat = (char *)va_arg( args, char * );
158             char *psz_name = (char *)va_arg( args, char * );
159
160             int i_ret = input_item_DelInfo( p_input->p->p_item,
161                                             psz_cat, psz_name );
162
163             if( !p_input->b_preparsing && !i_ret )
164                 input_SendEventMetaInfo( p_input );
165             return i_ret;
166         }
167         case INPUT_GET_INFO:
168         {
169             char *psz_cat = (char *)va_arg( args, char * );
170             char *psz_name = (char *)va_arg( args, char * );
171             char **ppsz_value = (char **)va_arg( args, char ** );
172             int i_ret = VLC_EGENERIC;
173             *ppsz_value = NULL;
174
175             *ppsz_value = input_item_GetInfo( p_input->p->p_item,
176                                                   psz_cat, psz_name );
177             return i_ret;
178         }
179
180         case INPUT_SET_NAME:
181         {
182             char *psz_name = (char *)va_arg( args, char * );
183
184             if( !psz_name ) return VLC_EGENERIC;
185
186             input_item_SetName( p_input->p->p_item, psz_name );
187
188             if( !p_input->b_preparsing )
189                 input_SendEventMetaName( p_input, psz_name );
190             return VLC_SUCCESS;
191         }
192
193         case INPUT_ADD_BOOKMARK:
194             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
195             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
196
197             vlc_mutex_lock( &p_input->p->p_item->lock );
198             if( !p_bkmk->psz_name )
199             {
200                  if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
201                                p_input->p->i_bookmark ) == -1 )
202                      p_bkmk->psz_name = NULL;
203             }
204
205             TAB_APPEND( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
206             vlc_mutex_unlock( &p_input->p->p_item->lock );
207
208             UpdateBookmarksOption( p_input );
209
210             return VLC_SUCCESS;
211
212         case INPUT_CHANGE_BOOKMARK:
213             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
214             i_bkmk = (int)va_arg( args, int );
215
216             vlc_mutex_lock( &p_input->p->p_item->lock );
217             if( i_bkmk < p_input->p->i_bookmark )
218             {
219                 vlc_seekpoint_Delete( p_input->p->pp_bookmark[i_bkmk] );
220                 p_input->p->pp_bookmark[i_bkmk] = vlc_seekpoint_Duplicate( p_bkmk );
221             }
222             vlc_mutex_unlock( &p_input->p->p_item->lock );
223
224             UpdateBookmarksOption( p_input );
225
226             return VLC_SUCCESS;
227
228         case INPUT_DEL_BOOKMARK:
229             i_bkmk = (int)va_arg( args, int );
230
231             vlc_mutex_lock( &p_input->p->p_item->lock );
232             if( i_bkmk < p_input->p->i_bookmark )
233             {
234                 p_bkmk = p_input->p->pp_bookmark[i_bkmk];
235                 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark, p_bkmk );
236                 vlc_seekpoint_Delete( p_bkmk );
237
238                 vlc_mutex_unlock( &p_input->p->p_item->lock );
239
240                 UpdateBookmarksOption( p_input );
241
242                 return VLC_SUCCESS;
243             }
244             vlc_mutex_unlock( &p_input->p->p_item->lock );
245
246             return VLC_EGENERIC;
247
248         case INPUT_GET_BOOKMARKS:
249             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
250             pi_bkmk = (int *)va_arg( args, int * );
251
252             vlc_mutex_lock( &p_input->p->p_item->lock );
253             if( p_input->p->i_bookmark )
254             {
255                 int i;
256
257                 *pi_bkmk = p_input->p->i_bookmark;
258                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
259                                     p_input->p->i_bookmark );
260                 for( i = 0; i < p_input->p->i_bookmark; i++ )
261                 {
262                     (*ppp_bkmk)[i] =
263                         vlc_seekpoint_Duplicate( p_input->p->pp_bookmark[i] );
264                 }
265
266                 vlc_mutex_unlock( &p_input->p->p_item->lock );
267                 return VLC_SUCCESS;
268             }
269             else
270             {
271                 *ppp_bkmk = NULL;
272                 *pi_bkmk = 0;
273
274                 vlc_mutex_unlock( &p_input->p->p_item->lock );
275                 return VLC_EGENERIC;
276             }
277             break;
278
279         case INPUT_CLEAR_BOOKMARKS:
280
281             vlc_mutex_lock( &p_input->p->p_item->lock );
282             while( p_input->p->i_bookmark > 0 )
283             {
284                 p_bkmk = p_input->p->pp_bookmark[p_input->p->i_bookmark-1];
285
286                 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->pp_bookmark,
287                             p_bkmk );
288                 vlc_seekpoint_Delete( p_bkmk );
289             }
290             vlc_mutex_unlock( &p_input->p->p_item->lock );
291
292             UpdateBookmarksOption( p_input );
293             return VLC_SUCCESS;
294
295         case INPUT_SET_BOOKMARK:
296             i_bkmk = (int)va_arg( args, int );
297
298             val.i_int = i_bkmk;
299             input_ControlPush( p_input, INPUT_CONTROL_SET_BOOKMARK, &val );
300
301             return VLC_SUCCESS;
302
303         case INPUT_GET_BOOKMARK:
304             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
305
306             vlc_mutex_lock( &p_input->p->p_item->lock );
307             *p_bkmk = p_input->p->bookmark;
308             vlc_mutex_unlock( &p_input->p->p_item->lock );
309             return VLC_SUCCESS;
310
311         case INPUT_ADD_OPTION:
312         {
313             const char *psz_option = va_arg( args, const char * );
314             const char *psz_value = va_arg( args, const char * );
315             char *str;
316             int i;
317
318             if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
319                 return VLC_ENOMEM;
320
321             i = input_item_AddOption( p_input->p->p_item, str,
322                                       VLC_INPUT_OPTION_UNIQUE );
323             free( str );
324             return i;
325         }
326
327         case INPUT_GET_VIDEO_FPS:
328             pf = (double*)va_arg( args, double * );
329
330             vlc_mutex_lock( &p_input->p->p_item->lock );
331             *pf = p_input->p->f_fps;
332             vlc_mutex_unlock( &p_input->p->p_item->lock );
333             return VLC_SUCCESS;
334
335         case INPUT_ADD_SLAVE:
336             psz = (char*)va_arg( args, char * );
337             if( psz && *psz )
338             {
339                 val.psz_string = strdup( psz );
340                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
341             }
342             return VLC_SUCCESS;
343
344         case INPUT_ADD_SUBTITLE:
345             psz = (char*)va_arg( args, char * );
346             b_bool = (bool)va_arg( args, int );
347
348             if( !psz || *psz == '\0' )
349                 return VLC_EGENERIC;
350             if( b_bool && !subtitles_Filter( psz ) )
351                 return VLC_EGENERIC;
352
353             val.psz_string = strdup( psz );
354             input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
355             return VLC_SUCCESS;
356
357         case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int*  res=can fail */
358         {
359             input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
360             int *pi_attachment = (int*)va_arg( args, int * );
361             int i;
362
363             vlc_mutex_lock( &p_input->p->p_item->lock );
364             if( p_input->p->i_attachment <= 0 )
365             {
366                 vlc_mutex_unlock( &p_input->p->p_item->lock );
367                 *ppp_attachment = NULL;
368                 *pi_attachment = 0;
369                 return VLC_EGENERIC;
370             }
371             *pi_attachment = p_input->p->i_attachment;
372             *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
373             for( i = 0; i < p_input->p->i_attachment; i++ )
374                 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
375
376             vlc_mutex_unlock( &p_input->p->p_item->lock );
377             return VLC_SUCCESS;
378         }
379
380         case INPUT_GET_ATTACHMENT:  /* arg1=input_attachment_t**, arg2=char*  res=can fail */
381         {
382             input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
383             const char *psz_name = (const char*)va_arg( args, const char * );
384             int i;
385
386             vlc_mutex_lock( &p_input->p->p_item->lock );
387             for( i = 0; i < p_input->p->i_attachment; i++ )
388             {
389                 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
390                 {
391                     *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
392                     vlc_mutex_unlock( &p_input->p->p_item->lock );
393                     return VLC_SUCCESS;
394                 }
395             }
396             *pp_attachment = NULL;
397             vlc_mutex_unlock( &p_input->p->p_item->lock );
398             return VLC_EGENERIC;
399         }
400
401         case INPUT_SET_RECORD_STATE:
402             b_bool = (bool)va_arg( args, int );
403             var_SetBool( p_input, "record", b_bool );
404             return VLC_SUCCESS;
405
406         case INPUT_GET_RECORD_STATE:
407             pb_bool = (bool*)va_arg( args, bool* );
408             *pb_bool = var_GetBool( p_input, "record" );
409             return VLC_SUCCESS;
410
411         case INPUT_RESTART_ES:
412             val.i_int = (int)va_arg( args, int );
413             input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
414             return VLC_SUCCESS;
415
416         case INPUT_GET_AOUT:
417         {
418             aout_instance_t *p_aout = input_resource_HoldAout( p_input->p->p_resource );
419             if( !p_aout )
420                 return VLC_EGENERIC;
421
422             aout_instance_t **pp_aout = (aout_instance_t**)va_arg( args, aout_instance_t** );
423             *pp_aout = p_aout;
424             return VLC_SUCCESS;
425         }
426
427         case INPUT_GET_VOUTS:
428         {
429             vout_thread_t ***ppp_vout = (vout_thread_t***)va_arg( args, vout_thread_t*** );
430             int           *pi_vout = (int*)va_arg( args, int* );
431
432             input_resource_HoldVouts( p_input->p->p_resource, ppp_vout, pi_vout );
433             if( *pi_vout <= 0 )
434                 return VLC_EGENERIC;
435             return VLC_SUCCESS;
436         }
437
438         default:
439             msg_Err( p_input, "unknown query in input_vaControl" );
440             return VLC_EGENERIC;
441     }
442 }
443
444 static void UpdateBookmarksOption( input_thread_t *p_input )
445 {
446     vlc_mutex_lock( &p_input->p->p_item->lock );
447
448     /* Update the "bookmark" list */
449     var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
450     for( int i = 0; i < p_input->p->i_bookmark; i++ )
451     {
452         vlc_value_t val, text;
453
454         val.i_int = i;
455         text.psz_string = p_input->p->pp_bookmark[i]->psz_name;
456         var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
457                     &val, &text );
458     }
459
460     /* Create the "bookmarks" option value */
461     const char *psz_format = "{name=%s,bytes=%"PRId64",time=%"PRId64"}";
462     int i_len = strlen( "bookmarks=" );
463     for( int i = 0; i < p_input->p->i_bookmark; i++ )
464     {
465         const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
466
467         i_len += snprintf( NULL, 0, psz_format,
468                            p_bookmark->psz_name,
469                            p_bookmark->i_byte_offset,
470                            p_bookmark->i_time_offset/1000000 );
471     }
472
473     char *psz_value = malloc( i_len + p_input->p->i_bookmark + 1 );
474     char *psz_next = psz_value;
475
476     psz_next += sprintf( psz_next, "bookmarks=" );
477     for( int i = 0; i < p_input->p->i_bookmark && psz_value != NULL; i++ )
478     {
479         const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[i];
480
481         psz_next += sprintf( psz_next, psz_format,
482                              p_bookmark->psz_name,
483                              p_bookmark->i_byte_offset,
484                              p_bookmark->i_time_offset/1000000 );
485
486         if( i < p_input->p->i_bookmark - 1)
487             *psz_next++ = ',';
488     }
489     vlc_mutex_unlock( &p_input->p->p_item->lock );
490
491     if( psz_value )
492         input_item_AddOption( p_input->p->p_item, psz_value, VLC_INPUT_OPTION_UNIQUE );
493
494     free( psz_value );
495
496     input_SendEventBookmark( p_input );
497 }
498