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