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