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