]> git.sesse.net Git - vlc/blob - src/input/control.c
* modules/stream_out/transcode.c, src/input/decoder.c, src/input/control.c: fixed...
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29 #include "vlc_playlist.h"
30
31
32 static void UpdateBookmarksOption( input_thread_t * );
33 static void NotifyPlaylist( input_thread_t * );
34
35 /****************************************************************************
36  * input_Control
37  ****************************************************************************/
38 /**
39  * Control function for inputs.
40  * \param p_input input handle
41  * \param i_query query type
42  * \return VLC_SUCCESS if ok
43  */
44 int input_Control( input_thread_t *p_input, int i_query, ...  )
45 {
46     va_list args;
47     int     i_result;
48
49     va_start( args, i_query );
50     i_result = input_vaControl( p_input, i_query, args );
51     va_end( args );
52
53     return i_result;
54 }
55
56 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
57 {
58     seekpoint_t *p_bkmk, ***ppp_bkmk;
59     int i_bkmk = 0;
60     int *pi_bkmk;
61
62     int i_int, *pi_int;
63     double f, *pf;
64     int64_t i_64, *pi_64;
65
66     char *psz;
67     vlc_value_t val;
68
69     switch( i_query )
70     {
71         case INPUT_GET_POSITION:
72             pf = (double*)va_arg( args, double * );
73             *pf = var_GetFloat( p_input, "position" );
74             return VLC_SUCCESS;
75
76         case INPUT_SET_POSITION:
77             f = (double)va_arg( args, double );
78             return var_SetFloat( p_input, "position", f );
79
80         case INPUT_GET_LENGTH:
81             pi_64 = (int64_t*)va_arg( args, int64_t * );
82             *pi_64 = var_GetTime( p_input, "length" );
83             return VLC_SUCCESS;
84
85         case INPUT_GET_TIME:
86             pi_64 = (int64_t*)va_arg( args, int64_t * );
87             *pi_64 = var_GetTime( p_input, "time" );
88             return VLC_SUCCESS;
89
90         case INPUT_SET_TIME:
91             i_64 = (int64_t)va_arg( args, int64_t );
92             return var_SetTime( p_input, "time", i_64 );
93
94         case INPUT_GET_RATE:
95             pi_int = (int*)va_arg( args, int * );
96             *pi_int = var_GetInteger( p_input, "rate" );
97             return VLC_SUCCESS;
98
99         case INPUT_SET_RATE:
100             i_int = (int)va_arg( args, int );
101             return var_SetInteger( p_input, "rate", i_int );
102
103         case INPUT_GET_STATE:
104             pi_int = (int*)va_arg( args, int * );
105             *pi_int = var_GetInteger( p_input, "state" );
106             return VLC_SUCCESS;
107
108         case INPUT_SET_STATE:
109             i_int = (int)va_arg( args, int );
110             return var_SetInteger( p_input, "state", i_int );
111
112         case INPUT_GET_AUDIO_DELAY:
113             pi_64 = (int64_t*)va_arg( args, int64_t * );
114             *pi_64 = var_GetTime( p_input, "audio-delay" );
115             return VLC_SUCCESS;
116
117         case INPUT_GET_SPU_DELAY:
118             pi_64 = (int64_t*)va_arg( args, int64_t * );
119             *pi_64 = var_GetTime( p_input, "spu-delay" );
120             return VLC_SUCCESS;
121
122         case INPUT_SET_AUDIO_DELAY:
123             i_64 = (int64_t)va_arg( args, int64_t );
124             return var_SetTime( p_input, "audio-delay", i_64 );
125
126         case INPUT_SET_SPU_DELAY:
127             i_64 = (int64_t)va_arg( args, int64_t );
128             return var_SetTime( p_input, "spu-delay", i_64 );
129
130         case INPUT_ADD_INFO:
131         {
132             /* FIXME : Impossible to use vlc_input_item_AddInfo because of
133              * the ... problem ? */
134             char *psz_cat = (char *)va_arg( args, char * );
135             char *psz_name = (char *)va_arg( args, char * );
136             char *psz_format = (char *)va_arg( args, char * );
137
138             info_category_t *p_cat;
139             info_t *p_info;
140             int i;
141
142             vlc_mutex_lock( &p_input->input.p_item->lock );
143             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
144             {
145                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
146                              psz_cat ) ) break;
147             }
148
149             if( i == p_input->input.p_item->i_categories )
150             {
151                 p_cat = malloc( sizeof( info_category_t ) );
152                 if( !p_cat )
153                 {
154                     vlc_mutex_lock( &p_input->input.p_item->lock );
155                     return VLC_EGENERIC;
156                 }
157                 p_cat->psz_name = strdup( psz_cat );
158                 p_cat->i_infos = 0;
159                 p_cat->pp_infos = NULL;
160                 INSERT_ELEM( p_input->input.p_item->pp_categories,
161                              p_input->input.p_item->i_categories,
162                              p_input->input.p_item->i_categories, p_cat );
163             }
164
165             p_cat = p_input->input.p_item->pp_categories[i];
166
167             for( i = 0; i < p_cat->i_infos; i++ )
168             {
169                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
170                 {
171                     if( p_cat->pp_infos[i]->psz_value )
172                         free( p_cat->pp_infos[i]->psz_value );
173                     break;
174                 }
175             }
176
177             if( i == p_cat->i_infos )
178             {
179                 p_info = malloc( sizeof( info_t ) );
180                 if( !p_info )
181                 {
182                     vlc_mutex_lock( &p_input->input.p_item->lock );
183                     return VLC_EGENERIC;
184                 }
185
186                 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
187                              p_cat->i_infos, p_info );
188                 p_info->psz_name = strdup( psz_name );
189             }
190
191             p_info = p_cat->pp_infos[i];
192             vasprintf( &p_info->psz_value, psz_format, args );
193
194             vlc_mutex_unlock( &p_input->input.p_item->lock );
195
196             NotifyPlaylist( p_input );
197         }
198         return VLC_SUCCESS;
199
200         case INPUT_DEL_INFO:
201         {
202             char *psz_cat = (char *)va_arg( args, char * );
203             char *psz_name = (char *)va_arg( args, char * );
204
205             info_category_t *p_cat = NULL;
206             int i;
207
208             vlc_mutex_lock( &p_input->input.p_item->lock );
209             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
210             {
211                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
212                              psz_cat ) )
213                 {
214                     p_cat = p_input->input.p_item->pp_categories[i];
215                     break;
216                 }
217             }
218             if( p_cat == NULL )
219             {
220                 vlc_mutex_unlock( &p_input->input.p_item->lock );
221                 return VLC_EGENERIC;
222             }
223
224             for( i = 0; i < p_cat->i_infos; i++ )
225             {
226                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
227                 {
228                     free( p_cat->pp_infos[i]->psz_name );
229                     if( p_cat->pp_infos[i]->psz_value )
230                         free( p_cat->pp_infos[i]->psz_value );
231                     free( p_cat->pp_infos[i] );
232                     REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
233                     break;
234                 }
235             }
236             vlc_mutex_unlock( &p_input->input.p_item->lock );
237
238             if( i >= p_cat->i_infos )
239                 return VLC_EGENERIC;
240
241             NotifyPlaylist( p_input );
242         }
243         return VLC_SUCCESS;
244
245
246         case INPUT_GET_INFO:
247         {
248             char *psz_cat = (char *)va_arg( args, char * );
249             char *psz_name = (char *)va_arg( args, char * );
250             char **ppsz_value = (char **)va_arg( args, char ** );
251             int i_ret = VLC_EGENERIC;
252             *ppsz_value = NULL;
253
254             *ppsz_value = vlc_input_item_GetInfo( p_input->input.p_item,
255                                                   psz_cat, psz_name );
256             return i_ret;
257         }
258
259         case INPUT_SET_NAME:
260         {
261             char *psz_name = (char *)va_arg( args, char * );
262
263             if( !psz_name ) return VLC_EGENERIC;
264
265             vlc_mutex_lock( &p_input->input.p_item->lock );
266             if( p_input->input.p_item->psz_name )
267                 free( p_input->input.p_item->psz_name );
268             p_input->input.p_item->psz_name = strdup( psz_name );
269             vlc_mutex_unlock( &p_input->input.p_item->lock );
270
271             NotifyPlaylist( p_input );
272
273             return VLC_SUCCESS;
274         }
275
276         case INPUT_ADD_BOOKMARK:
277             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
278             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
279
280             vlc_mutex_lock( &p_input->input.p_item->lock );
281             if( !p_bkmk->psz_name )
282             {
283                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
284                            p_input->i_bookmark );
285             }
286
287             TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
288
289             /* Reflect the changes on the object var */
290             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
291             {
292                 vlc_value_t val, text;
293                 int i;
294
295                 for( i = 0; i < p_input->i_bookmark; i++ )
296                 {
297                     val.i_int = i;
298                     text.psz_string = p_input->bookmark[i]->psz_name;
299                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
300                                 &val, &text );
301                 }
302             }
303             vlc_mutex_unlock( &p_input->input.p_item->lock );
304
305             UpdateBookmarksOption( p_input );
306
307             return VLC_SUCCESS;
308
309         case INPUT_CHANGE_BOOKMARK:
310             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
311             i_bkmk = (int)va_arg( args, int );
312
313             vlc_mutex_lock( &p_input->input.p_item->lock );
314             if( i_bkmk < p_input->i_bookmark )
315             {
316                 vlc_value_t val, text;
317                 int i;
318
319                 p_input->bookmark[i_bkmk] = p_bkmk;
320
321                 /* Reflect the changes on the object var */
322                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
323                 for( i = 0; i < p_input->i_bookmark; i++ )
324                 {
325                     val.i_int = i;
326                     text.psz_string = p_input->bookmark[i]->psz_name;
327                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
328                                 &val, &text );
329                 }
330             }
331             vlc_mutex_unlock( &p_input->input.p_item->lock );
332
333             UpdateBookmarksOption( p_input );
334
335             return VLC_SUCCESS;
336
337         case INPUT_DEL_BOOKMARK:
338             i_bkmk = (int)va_arg( args, int );
339
340             vlc_mutex_lock( &p_input->input.p_item->lock );
341             if( i_bkmk < p_input->i_bookmark )
342             {
343                 vlc_value_t val, text;
344                 int i;
345
346                 p_bkmk = p_input->bookmark[i_bkmk];
347                 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
348                             p_bkmk );
349                 vlc_seekpoint_Delete( p_bkmk );
350
351                 /* Reflect the changes on the object var */
352                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
353                 for( i = 0; i < p_input->i_bookmark; i++ )
354                 {
355                     val.i_int = i;
356                     text.psz_string = p_input->bookmark[i]->psz_name;
357                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
358                                 &val, &text );
359                 }
360                 vlc_mutex_unlock( &p_input->input.p_item->lock );
361
362                 UpdateBookmarksOption( p_input );
363
364                 return VLC_SUCCESS;
365             }
366             vlc_mutex_unlock( &p_input->input.p_item->lock );
367
368             return VLC_EGENERIC;
369
370         case INPUT_GET_BOOKMARKS:
371             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
372             pi_bkmk = (int *)va_arg( args, int * );
373
374             vlc_mutex_lock( &p_input->input.p_item->lock );
375             if( p_input->i_bookmark )
376             {
377                 int i;
378
379                 *pi_bkmk = p_input->i_bookmark;
380                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
381                                     p_input->i_bookmark );
382                 for( i = 0; i < p_input->i_bookmark; i++ )
383                 {
384                     (*ppp_bkmk)[i] =
385                         vlc_seekpoint_Duplicate(p_input->bookmark[i]);
386                 }
387
388                 vlc_mutex_unlock( &p_input->input.p_item->lock );
389                 return VLC_SUCCESS;
390             }
391             else
392             {
393                 *ppp_bkmk = NULL;
394                 *pi_bkmk = 0;
395
396                 vlc_mutex_unlock( &p_input->input.p_item->lock );
397                 return VLC_EGENERIC;
398             }
399             break;
400
401         case INPUT_CLEAR_BOOKMARKS:
402
403             vlc_mutex_lock( &p_input->input.p_item->lock );
404             if( p_input->i_bookmark )
405             {
406                 int i;
407
408                 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
409                 {
410                     p_bkmk = p_input->bookmark[i];
411                     TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
412                                 p_bkmk );
413                     vlc_seekpoint_Delete( p_bkmk );
414                 }
415                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
416             }
417             vlc_mutex_unlock( &p_input->input.p_item->lock );
418
419             UpdateBookmarksOption( p_input );
420
421             return VLC_SUCCESS;
422
423         case INPUT_SET_BOOKMARK:
424             i_bkmk = (int)va_arg( args, int );
425
426             vlc_mutex_lock( &p_input->input.p_item->lock );
427             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
428             {
429                 vlc_value_t pos;
430                 int i_ret;
431
432                 if( p_input->bookmark[i_bkmk]->i_time_offset != -1 )
433                 {
434                     pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
435                     i_ret = var_Set( p_input, "time", pos );
436                 }
437                 else if( p_input->bookmark[i_bkmk]->i_byte_offset != -1 )
438                 {
439                     // don't crash on bookmarks in live streams
440                     if( stream_Size( p_input->input.p_stream ) == 0 )
441                     {
442                         vlc_mutex_unlock( &p_input->input.p_item->lock );
443                         return VLC_EGENERIC;
444                     }
445                     pos.f_float = !p_input->input.p_stream ? 0 :
446                         p_input->bookmark[i_bkmk]->i_byte_offset /
447                         stream_Size( p_input->input.p_stream );
448                     i_ret = var_Set( p_input, "position", pos );
449                 }
450                 else
451                 {
452                     pos.f_float = 0;
453                     i_ret = var_Set( p_input, "position", pos );
454                 }
455
456                 vlc_mutex_unlock( &p_input->input.p_item->lock );
457                 return i_ret;
458             }
459             else
460             {
461                 vlc_mutex_unlock( &p_input->input.p_item->lock );
462                 return VLC_EGENERIC;
463             }
464
465             break;
466
467         case INPUT_ADD_OPTION:
468         {
469             char *psz_option = (char *)va_arg( args, char * );
470             char *psz_value = (char *)va_arg( args, char * );
471             int i;
472
473             vlc_mutex_lock( &p_input->input.p_item->lock );
474             /* Check if option already exists */
475             for( i = 0; i < p_input->input.p_item->i_options; i++ )
476             {
477                 if( !strncmp( p_input->input.p_item->ppsz_options[i],
478                               psz_option, strlen( psz_option ) ) &&
479                     p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
480                       == '=' )
481                 {
482                     free( p_input->input.p_item->ppsz_options[i] );
483                     break;
484                 }
485             }
486             if( i == p_input->input.p_item->i_options )
487             {
488                 p_input->input.p_item->i_options++;
489                 p_input->input.p_item->ppsz_options =
490                     realloc( p_input->input.p_item->ppsz_options,
491                              p_input->input.p_item->i_options *
492                              sizeof(char **) );
493             }
494
495             asprintf( &p_input->input.p_item->ppsz_options[i],
496                       "%s=%s", psz_option, psz_value ) ;
497             vlc_mutex_unlock( &p_input->input.p_item->lock );
498
499             return VLC_SUCCESS;
500         }
501
502         case INPUT_GET_BYTE_POSITION:
503             pi_64 = (int64_t*)va_arg( args, int64_t * );
504             *pi_64 = !p_input->input.p_stream ? 0 :
505                 stream_Tell( p_input->input.p_stream );
506             return VLC_SUCCESS;
507
508         case INPUT_SET_BYTE_SIZE:
509             pi_64 = (int64_t*)va_arg( args, int64_t * );
510             *pi_64 = !p_input->input.p_stream ? 0 :
511                 stream_Size( p_input->input.p_stream );
512             return VLC_SUCCESS;
513
514         case INPUT_ADD_SLAVE:
515             psz = (char*)va_arg( args, char * );
516             if( psz && *psz )
517             {
518                 val.psz_string = strdup( psz );
519                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
520             }
521             return VLC_SUCCESS;
522
523         default:
524             msg_Err( p_input, "unknown query in input_vaControl" );
525             return VLC_EGENERIC;
526     }
527 }
528
529 static void NotifyPlaylist( input_thread_t *p_input )
530 {
531     playlist_t *p_playlist =
532         (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
533                                        FIND_PARENT );
534     if( p_playlist )
535     {
536         var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
537         vlc_object_release( p_playlist );
538     }
539 }
540
541 static void UpdateBookmarksOption( input_thread_t *p_input )
542 {
543     int i, i_len = 0;
544     char *psz_value = NULL, *psz_next = NULL;
545
546     vlc_mutex_lock( &p_input->input.p_item->lock );
547     for( i = 0; i < p_input->i_bookmark; i++ )
548     {
549         asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
550                   p_input->bookmark[i]->psz_name,
551                   p_input->bookmark[i]->i_byte_offset,
552                   p_input->bookmark[i]->i_time_offset/1000000 );
553         i_len += strlen( psz_value );
554         free( psz_value );
555     }
556     for( i = 0; i < p_input->i_bookmark; i++ )
557     {
558         if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
559
560         sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
561                  p_input->bookmark[i]->psz_name,
562                  p_input->bookmark[i]->i_byte_offset,
563                  p_input->bookmark[i]->i_time_offset/1000000 );
564
565         psz_next += strlen( psz_next );
566         if( i < p_input->i_bookmark - 1)
567         {
568             *psz_next = ','; psz_next++;
569         }
570     }
571     vlc_mutex_unlock( &p_input->input.p_item->lock );
572
573     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
574                    psz_value ? psz_value : "" );
575 }