video.c: don't display pre-rolled pictures
[vlc.git] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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
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_GET_INFO:
201         {
202             char *psz_cat = (char *)va_arg( args, char * );
203             char *psz_name = (char *)va_arg( args, char * );
204             char **ppsz_value = (char **)va_arg( args, char ** );
205             int i_ret = VLC_EGENERIC;
206             int i;
207             *ppsz_value = NULL;
208
209             *ppsz_value = vlc_input_item_GetInfo( p_input->input.p_item,
210                                                   psz_cat, psz_name );
211             return i_ret;
212         }
213
214         case INPUT_SET_NAME:
215         {
216             char *psz_name = (char *)va_arg( args, char * );
217
218             if( !psz_name ) return VLC_EGENERIC;
219
220             vlc_mutex_lock( &p_input->input.p_item->lock );
221             if( p_input->input.p_item->psz_name )
222                 free( p_input->input.p_item->psz_name );
223             p_input->input.p_item->psz_name = strdup( psz_name );
224             vlc_mutex_unlock( &p_input->input.p_item->lock );
225
226             NotifyPlaylist( p_input );
227
228             return VLC_SUCCESS;
229         }
230
231         case INPUT_ADD_BOOKMARK:
232             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
233             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
234
235             vlc_mutex_lock( &p_input->input.p_item->lock );
236             if( !p_bkmk->psz_name )
237             {
238                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
239                            p_input->i_bookmark );
240             }
241
242             TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
243
244             /* Reflect the changes on the object var */
245             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
246             {
247                 vlc_value_t val, text;
248                 int i;
249
250                 for( i = 0; i < p_input->i_bookmark; i++ )
251                 {
252                     val.i_int = i;
253                     text.psz_string = p_input->bookmark[i]->psz_name;
254                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
255                                 &val, &text );
256                 }
257             }
258             vlc_mutex_unlock( &p_input->input.p_item->lock );
259
260             UpdateBookmarksOption( p_input );
261
262             return VLC_SUCCESS;
263
264         case INPUT_CHANGE_BOOKMARK:
265             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
266             i_bkmk = (int)va_arg( args, int );
267
268             vlc_mutex_lock( &p_input->input.p_item->lock );
269             if( i_bkmk < p_input->i_bookmark )
270             {
271                 vlc_value_t val, text;
272                 int i;
273
274                 p_input->bookmark[i_bkmk] = p_bkmk;
275
276                 /* Reflect the changes on the object var */
277                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
278                 for( i = 0; i < p_input->i_bookmark; i++ )
279                 {
280                     val.i_int = i;
281                     text.psz_string = p_input->bookmark[i]->psz_name;
282                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
283                                 &val, &text );
284                 }
285             }
286             vlc_mutex_unlock( &p_input->input.p_item->lock );
287
288             UpdateBookmarksOption( p_input );
289
290             return VLC_SUCCESS;
291
292         case INPUT_DEL_BOOKMARK:
293             i_bkmk = (int)va_arg( args, int );
294
295             vlc_mutex_lock( &p_input->input.p_item->lock );
296             if( i_bkmk < p_input->i_bookmark )
297             {
298                 vlc_value_t val, text;
299                 int i;
300
301                 p_bkmk = p_input->bookmark[i_bkmk];
302                 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
303                             p_bkmk );
304                 vlc_seekpoint_Delete( p_bkmk );
305
306                 /* Reflect the changes on the object var */
307                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
308                 for( i = 0; i < p_input->i_bookmark; i++ )
309                 {
310                     val.i_int = i;
311                     text.psz_string = p_input->bookmark[i]->psz_name;
312                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
313                                 &val, &text );
314                 }
315                 vlc_mutex_unlock( &p_input->input.p_item->lock );
316
317                 UpdateBookmarksOption( p_input );
318
319                 return VLC_SUCCESS;
320             }
321             vlc_mutex_unlock( &p_input->input.p_item->lock );
322
323             return VLC_EGENERIC;
324
325         case INPUT_GET_BOOKMARKS:
326             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
327             pi_bkmk = (int *)va_arg( args, int * );
328
329             vlc_mutex_lock( &p_input->input.p_item->lock );
330             if( p_input->i_bookmark )
331             {
332                 int i;
333
334                 *pi_bkmk = p_input->i_bookmark;
335                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
336                                     p_input->i_bookmark );
337                 for( i = 0; i < p_input->i_bookmark; i++ )
338                 {
339                     (*ppp_bkmk)[i] =
340                         vlc_seekpoint_Duplicate(p_input->bookmark[i]);
341                 }
342
343                 vlc_mutex_unlock( &p_input->input.p_item->lock );
344                 return VLC_SUCCESS;
345             }
346             else
347             {
348                 *ppp_bkmk = NULL;
349                 *pi_bkmk = 0;
350
351                 vlc_mutex_unlock( &p_input->input.p_item->lock );
352                 return VLC_EGENERIC;
353             }
354             break;
355
356         case INPUT_CLEAR_BOOKMARKS:
357
358             vlc_mutex_lock( &p_input->input.p_item->lock );
359             if( p_input->i_bookmark )
360             {
361                 int i;
362
363                 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
364                 {
365                     p_bkmk = p_input->bookmark[i];
366                     TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
367                                 p_bkmk );
368                     vlc_seekpoint_Delete( p_bkmk );
369                 }
370                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
371             }
372             vlc_mutex_unlock( &p_input->input.p_item->lock );
373
374             UpdateBookmarksOption( p_input );
375
376             return VLC_SUCCESS;
377
378         case INPUT_SET_BOOKMARK:
379             i_bkmk = (int)va_arg( args, int );
380
381             vlc_mutex_lock( &p_input->input.p_item->lock );
382             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
383             {
384                 vlc_value_t pos;
385                 int i_ret;
386
387                 if( p_input->bookmark[i_bkmk]->i_time_offset )
388                 {
389                     pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
390                     i_ret = var_Set( p_input, "time", pos );
391                 }
392                 else if( p_input->bookmark[i_bkmk]->i_byte_offset )
393                 {
394                     // don't crash on bookmarks in live streams
395                     if( stream_Size( p_input->input.p_stream ) == 0 )
396                     {
397                         vlc_mutex_unlock( &p_input->input.p_item->lock );
398                         return VLC_EGENERIC;
399                     }
400                     pos.f_float = !p_input->input.p_stream ? 0 :
401                         p_input->bookmark[i_bkmk]->i_byte_offset /
402                         stream_Size( p_input->input.p_stream );
403                     i_ret = var_Set( p_input, "position", pos );
404                 }
405                 else
406                 {
407                     pos.f_float = 0;
408                     i_ret = var_Set( p_input, "position", pos );
409                 }
410
411                 vlc_mutex_unlock( &p_input->input.p_item->lock );
412                 return i_ret;
413             }
414             else
415             {
416                 vlc_mutex_unlock( &p_input->input.p_item->lock );
417                 return VLC_EGENERIC;
418             }
419
420             break;
421
422         case INPUT_ADD_OPTION:
423         {
424             char *psz_option = (char *)va_arg( args, char * );
425             char *psz_value = (char *)va_arg( args, char * );
426             int i;
427
428             vlc_mutex_lock( &p_input->input.p_item->lock );
429             /* Check if option already exists */
430             for( i = 0; i < p_input->input.p_item->i_options; i++ )
431             {
432                 if( !strncmp( p_input->input.p_item->ppsz_options[i],
433                               psz_option, strlen( psz_option ) ) &&
434                     p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
435                       == '=' )
436                 {
437                     free( p_input->input.p_item->ppsz_options[i] );
438                     break;
439                 }
440             }
441             if( i == p_input->input.p_item->i_options )
442             {
443                 p_input->input.p_item->i_options++;
444                 p_input->input.p_item->ppsz_options =
445                     realloc( p_input->input.p_item->ppsz_options,
446                              p_input->input.p_item->i_options *
447                              sizeof(char **) );
448             }
449
450             asprintf( &p_input->input.p_item->ppsz_options[i],
451                       "%s=%s", psz_option, psz_value ) ;
452             vlc_mutex_unlock( &p_input->input.p_item->lock );
453
454             return VLC_SUCCESS;
455         }
456
457         case INPUT_GET_BYTE_POSITION:
458             pi_64 = (int64_t*)va_arg( args, int64_t * );
459             *pi_64 = !p_input->input.p_stream ? 0 :
460                 stream_Tell( p_input->input.p_stream );
461             return VLC_SUCCESS;
462
463         case INPUT_SET_BYTE_SIZE:
464             pi_64 = (int64_t*)va_arg( args, int64_t * );
465             *pi_64 = !p_input->input.p_stream ? 0 :
466                 stream_Size( p_input->input.p_stream );
467             return VLC_SUCCESS;
468
469         case INPUT_ADD_SLAVE:
470             psz = (char*)va_arg( args, char * );
471             if( psz && *psz )
472             {
473                 val.psz_string = strdup( psz );
474                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
475             }
476             return VLC_SUCCESS;
477
478         default:
479             msg_Err( p_input, "unknown query in input_vaControl" );
480             return VLC_EGENERIC;
481     }
482 }
483
484 static void NotifyPlaylist( input_thread_t *p_input )
485 {
486     playlist_t *p_playlist =
487         (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
488                                        FIND_PARENT );
489     if( p_playlist )
490     {
491         var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
492         vlc_object_release( p_playlist );
493     }
494 }
495
496 static void UpdateBookmarksOption( input_thread_t *p_input )
497 {
498     int i, i_len = 0;
499     char *psz_value = NULL, *psz_next = NULL;
500
501     vlc_mutex_lock( &p_input->input.p_item->lock );
502     for( i = 0; i < p_input->i_bookmark; i++ )
503     {
504         asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
505                   p_input->bookmark[i]->psz_name,
506                   p_input->bookmark[i]->i_byte_offset,
507                   p_input->bookmark[i]->i_time_offset/1000000 );
508         i_len += strlen( psz_value );
509         free( psz_value );
510     }
511     for( i = 0; i < p_input->i_bookmark; i++ )
512     {
513         if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
514
515         sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
516                  p_input->bookmark[i]->psz_name,
517                  p_input->bookmark[i]->i_byte_offset,
518                  p_input->bookmark[i]->i_time_offset/1000000 );
519
520         psz_next += strlen( psz_next );
521         if( i < p_input->i_bookmark - 1)
522         {
523             *psz_next = ','; psz_next++;
524         }
525     }
526     vlc_mutex_unlock( &p_input->input.p_item->lock );
527
528     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
529                    psz_value ? psz_value : "" );
530 }