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