Use the normal vlm lock to protect access to the condition variable.
[vlc.git] / src / input / vlm.c
1 /*****************************************************************************
2  * vlm.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <stdio.h>
36 #include <ctype.h>                                              /* tolower() */
37 #include <assert.h>
38
39 #include <vlc_vlm.h>
40
41 #ifndef WIN32
42 #   include <sys/time.h>                                   /* gettimeofday() */
43 #endif
44
45 #ifdef UNDER_CE
46 #include <sys/time.h>                                      /* gettimeofday() */
47 #endif
48
49 #include <time.h>                                                 /* ctime() */
50 #if defined (WIN32) && !defined (UNDER_CE)
51 #include <sys/timeb.h>                                            /* ftime() */
52 #endif
53
54 #include <vlc_input.h>
55 #include <vlc_stream.h>
56 #include "vlm_internal.h"
57 #include "vlm_event.h"
58 #include <vlc_vod.h>
59 #include <vlc_sout.h>
60 #include "../stream_output/stream_output.h"
61 #include "../libvlc.h"
62
63 /*****************************************************************************
64  * Local prototypes.
65  *****************************************************************************/
66
67 static void vlm_Destructor( vlm_t *p_vlm );
68 static void* Manage( void * );
69 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
70
71 static int InputEventPreparse( vlc_object_t *p_this, char const *psz_cmd,
72                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
73 {
74     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
75     vlc_sem_t *p_sem_preparse = p_data;
76
77     if( newval.i_int == INPUT_EVENT_DEAD ||
78         newval.i_int == INPUT_EVENT_ITEM_META )
79         vlc_sem_post( p_sem_preparse );
80
81     return VLC_SUCCESS;
82 }
83
84 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
85                        vlc_value_t oldval, vlc_value_t newval,
86                        void *p_data )
87 {
88     VLC_UNUSED(psz_cmd);
89     VLC_UNUSED(oldval);
90     input_thread_t *p_input = (input_thread_t *)p_this;
91     vlm_t *p_vlm = libvlc_priv( p_input->p_libvlc )->p_vlm;
92     assert( p_vlm );
93     vlm_media_sys_t *p_media = p_data;
94     const char *psz_instance_name = NULL;
95
96     if( newval.i_int == INPUT_EVENT_STATE )
97     {
98         for( int i = 0; i < p_media->i_instance; i++ )
99         {
100             if( p_media->instance[i]->p_input == p_input )
101             {
102                 psz_instance_name = p_media->instance[i]->psz_name;
103                 break;
104             }
105         }
106         vlm_SendEventMediaInstanceState( p_vlm, p_media->cfg.id, p_media->cfg.psz_name, psz_instance_name, var_GetInteger( p_input, "state" ) );
107
108         vlc_mutex_lock( &p_vlm->lock );
109         vlc_cond_signal( &p_vlm->wait );
110         vlc_mutex_unlock( &p_vlm->lock );
111     }
112     return VLC_SUCCESS;
113 }
114
115 static vlc_mutex_t vlm_mutex = VLC_STATIC_MUTEX;
116
117 #undef vlm_New
118 /*****************************************************************************
119  * vlm_New:
120  *****************************************************************************/
121 vlm_t *vlm_New ( vlc_object_t *p_this )
122 {
123     vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
124     char *psz_vlmconf;
125     static const char vlm_object_name[] = "vlm daemon";
126
127     /* Avoid multiple creation */
128     vlc_mutex_lock( &vlm_mutex );
129
130     p_vlm = *pp_vlm;
131     if( p_vlm )
132     {   /* VLM already exists */
133         vlc_object_hold( p_vlm );
134         vlc_mutex_unlock( &vlm_mutex );
135         return p_vlm;
136     }
137
138     msg_Dbg( p_this, "creating VLM" );
139
140     p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
141                                vlm_object_name );
142     if( !p_vlm )
143     {
144         vlc_mutex_unlock( &vlm_mutex );
145         return NULL;
146     }
147
148     vlc_mutex_init( &p_vlm->lock );
149     vlc_mutex_init( &p_vlm->lock );
150     vlc_cond_init_daytime( &p_vlm->wait );
151     p_vlm->i_id = 1;
152     TAB_INIT( p_vlm->i_media, p_vlm->media );
153     TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
154     p_vlm->i_vod = 0;
155     p_vlm->p_vod = NULL;
156     var_Create( p_vlm, "intf-event", VLC_VAR_ADDRESS );
157     vlc_object_attach( p_vlm, p_this->p_libvlc );
158
159     if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
160     {
161         vlc_cond_destroy( &p_vlm->wait );
162         vlc_mutex_destroy( &p_vlm->lock );
163         vlc_mutex_destroy( &p_vlm->lock );
164         vlc_object_release( p_vlm );
165         vlc_mutex_unlock( &vlm_mutex );
166         return NULL;
167     }
168
169     *pp_vlm = p_vlm; /* for future reference */
170
171     /* Load our configuration file */
172     psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
173     if( psz_vlmconf && *psz_vlmconf )
174     {
175         vlm_message_t *p_message = NULL;
176         char *psz_buffer = NULL;
177
178         msg_Dbg( p_this, "loading VLM configuration" );
179         if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) != -1 )
180         {
181             msg_Dbg( p_this, "%s", psz_buffer );
182             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
183                 msg_Warn( p_this, "error while loading the configuration file" );
184
185             vlm_MessageDelete( p_message );
186             free( psz_buffer );
187         }
188     }
189     free( psz_vlmconf );
190
191     vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
192     vlc_mutex_unlock( &vlm_mutex );
193
194     return p_vlm;
195 }
196
197 /*****************************************************************************
198  * vlm_Delete:
199  *****************************************************************************/
200 void vlm_Delete( vlm_t *p_vlm )
201 {
202     /* vlm_Delete() is serialized against itself, and against vlm_New().
203      * This way, vlm_Destructor () (called from vlc_objet_release() above)
204      * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
205     vlc_mutex_lock( &vlm_mutex );
206     vlc_object_release( p_vlm );
207     vlc_mutex_unlock( &vlm_mutex );
208 }
209
210 /*****************************************************************************
211  * vlm_Destructor:
212  *****************************************************************************/
213 static void vlm_Destructor( vlm_t *p_vlm )
214 {
215     vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
216     TAB_CLEAN( p_vlm->i_media, p_vlm->media );
217
218     vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
219     TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
220
221     vlc_mutex_lock( &p_vlm->lock );
222     vlc_cond_signal( &p_vlm->wait );
223     vlc_mutex_unlock( &p_vlm->lock );
224
225     libvlc_priv(p_vlm->p_libvlc)->p_vlm = NULL;
226     vlc_object_kill( p_vlm );
227     /*vlc_cancel( p_vlm->thread ); */
228     vlc_join( p_vlm->thread, NULL );
229
230     vlc_cond_destroy( &p_vlm->wait );
231     vlc_mutex_destroy( &p_vlm->lock );
232     vlc_mutex_destroy( &p_vlm->lock );
233 }
234
235 /*****************************************************************************
236  * vlm_ExecuteCommand:
237  *****************************************************************************/
238 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
239                         vlm_message_t **pp_message)
240 {
241     int i_result;
242
243     vlc_mutex_lock( &p_vlm->lock );
244     i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
245     vlc_mutex_unlock( &p_vlm->lock );
246
247     return i_result;
248 }
249
250
251 int64_t vlm_Date(void)
252 {
253 #if defined (WIN32) && !defined (UNDER_CE)
254     struct timeb tm;
255     ftime( &tm );
256     return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
257 #else
258     struct timeval tv_date;
259
260     /* gettimeofday() cannot fail given &tv_date is a valid address */
261     (void)gettimeofday( &tv_date, NULL );
262     return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
263 #endif
264 }
265
266
267 /*****************************************************************************
268  *
269  *****************************************************************************/
270 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
271                                 const char *psz_id, int i_query, va_list args )
272 {
273     vlm_t *vlm = (vlm_t *)p_private;
274     int i, i_ret;
275     const char *psz;
276     int64_t id;
277
278     if( !p_private || !p_vod_media )
279         return VLC_EGENERIC;
280
281     vlc_mutex_lock( &vlm->lock );
282
283     /* Find media id */
284     for( i = 0, id = -1; i < vlm->i_media; i++ )
285     {
286         if( p_vod_media == vlm->media[i]->vod.p_media )
287         {
288             id = vlm->media[i]->cfg.id;
289             break;
290         }
291     }
292     if( id == -1 )
293     {
294         vlc_mutex_unlock( &vlm->lock );
295         return VLC_EGENERIC;
296     }
297
298     switch( i_query )
299     {
300     case VOD_MEDIA_PLAY:
301         psz = (const char *)va_arg( args, const char * );
302         i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
303         break;
304
305     case VOD_MEDIA_PAUSE:
306         i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
307         break;
308
309     case VOD_MEDIA_STOP:
310         i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
311         break;
312
313     case VOD_MEDIA_SEEK:
314     {
315         double d_position = (double)va_arg( args, double );
316         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
317         break;
318     }
319
320     case VOD_MEDIA_REWIND:
321     {
322         double d_scale = (double)va_arg( args, double );
323         double d_position;
324
325         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
326         d_position -= (d_scale / 1000.0);
327         if( d_position < 0.0 )
328             d_position = 0.0;
329         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
330         break;
331     }
332
333     case VOD_MEDIA_FORWARD:
334     {
335         double d_scale = (double)va_arg( args, double );
336         double d_position;
337
338         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
339         d_position += (d_scale / 1000.0);
340         if( d_position > 1.0 )
341             d_position = 1.0;
342         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
343         break;
344     }
345
346     default:
347         i_ret = VLC_EGENERIC;
348         break;
349     }
350
351     vlc_mutex_unlock( &vlm->lock );
352
353     return i_ret;
354 }
355
356
357 /*****************************************************************************
358  * Manage:
359  *****************************************************************************/
360 static void* Manage( void* p_object )
361 {
362     vlm_t *vlm = (vlm_t*)p_object;
363     int i, j;
364     mtime_t i_lastcheck;
365     mtime_t i_time;
366     mtime_t i_nextschedule = 0;
367
368     int canc = vlc_savecancel ();
369     i_lastcheck = vlm_Date();
370
371     while( !vlm->b_die )
372     {
373         char **ppsz_scheduled_commands = NULL;
374         int    i_scheduled_commands = 0;
375
376         vlc_mutex_lock( &vlm->lock );
377         if( i_nextschedule )
378             vlc_cond_timedwait( &vlm->wait, &vlm->lock, i_nextschedule );
379         else
380             vlc_cond_wait( &vlm->wait, &vlm->lock );
381
382         /* destroy the inputs that wants to die, and launch the next input */
383         for( i = 0; i < vlm->i_media; i++ )
384         {
385             vlm_media_sys_t *p_media = vlm->media[i];
386
387             for( j = 0; j < p_media->i_instance; )
388             {
389                 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
390
391                 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
392                 {
393                     int i_new_input_index;
394
395                     /* */
396                     i_new_input_index = p_instance->i_index + 1;
397                     if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
398                         i_new_input_index = 0;
399
400                     /* FIXME implement multiple input with VOD */
401                     if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
402                         vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
403                     else
404                         vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
405
406                     j = 0;
407                 }
408                 else
409                 {
410                     j++;
411                 }
412             }
413         }
414
415         /* scheduling */
416         i_time = vlm_Date();
417         i_nextschedule = 0;
418
419         for( i = 0; i < vlm->i_schedule; i++ )
420         {
421             mtime_t i_real_date = vlm->schedule[i]->i_date;
422
423             if( vlm->schedule[i]->b_enabled == true )
424             {
425                 if( vlm->schedule[i]->i_date == 0 ) // now !
426                 {
427                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
428                     i_real_date = i_time;
429                 }
430                 else if( vlm->schedule[i]->i_period != 0 )
431                 {
432                     int j = 0;
433                     while( vlm->schedule[i]->i_date + j *
434                            vlm->schedule[i]->i_period <= i_lastcheck &&
435                            ( vlm->schedule[i]->i_repeat > j ||
436                              vlm->schedule[i]->i_repeat == -1 ) )
437                     {
438                         j++;
439                     }
440
441                     i_real_date = vlm->schedule[i]->i_date + j *
442                         vlm->schedule[i]->i_period;
443                 }
444
445                 if( i_real_date <= i_time )
446                 {
447                     if( i_real_date > i_lastcheck )
448                     {
449                         for( j = 0; j < vlm->schedule[i]->i_command; j++ )
450                         {
451                             TAB_APPEND( i_scheduled_commands,
452                                         ppsz_scheduled_commands,
453                                         strdup(vlm->schedule[i]->command[j] ) );
454                         }
455                     }
456                 }
457                 else
458                 {
459                     i_nextschedule = i_real_date;
460                 }
461             }
462         }
463
464         while( i_scheduled_commands )
465         {
466             vlm_message_t *message = NULL;
467             char *psz_command = ppsz_scheduled_commands[0];
468             ExecuteCommand( vlm, psz_command,&message );
469
470             /* for now, drop the message */
471             vlm_MessageDelete( message );
472             TAB_REMOVE( i_scheduled_commands,
473                         ppsz_scheduled_commands,
474                         psz_command );
475             free( psz_command );
476         }
477
478         i_lastcheck = i_time;
479
480         vlc_mutex_unlock( &vlm->lock );
481     }
482
483     vlc_restorecancel (canc);
484     return NULL;
485 }
486
487 /* New API
488  */
489 /*
490 typedef struct
491 {
492     struct
493     {
494         int i_connection_count;
495         int i_connection_active;
496     } vod;
497     struct
498     {
499         int        i_count;
500         bool b_playing;
501         int        i_playing_index;
502     } broadcast;
503
504 } vlm_media_status_t;
505 */
506
507 /* */
508 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
509 {
510     int i;
511
512     for( i = 0; i < p_vlm->i_media; i++ )
513     {
514         if( p_vlm->media[i]->cfg.id == id )
515             return p_vlm->media[i];
516     }
517     return NULL;
518 }
519 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
520 {
521     int i;
522
523     for( i = 0; i < p_vlm->i_media; i++ )
524     {
525         if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
526             return p_vlm->media[i];
527     }
528     return NULL;
529 }
530 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
531 {
532     int i;
533
534     if( !p_cfg || !p_cfg->psz_name ||
535         !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
536         return VLC_EGENERIC;
537
538     for( i = 0; i < p_vlm->i_media; i++ )
539     {
540         if( p_vlm->media[i]->cfg.id == p_cfg->id )
541             continue;
542         if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
543             return VLC_EGENERIC;
544     }
545     return VLC_SUCCESS;
546 }
547
548
549 /* Called after a media description is changed/added */
550 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
551 {
552     vlm_media_t *p_cfg = &p_media->cfg;
553     /* Check if we need to create/delete a vod media */
554     if( p_cfg->b_vod )
555     {
556         if( !p_cfg->b_enabled && p_media->vod.p_media )
557         {
558             p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
559             p_media->vod.p_media = NULL;
560         }
561         else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
562         {
563             /* Pre-parse the input */
564             input_thread_t *p_input;
565             char *psz_output;
566             char *psz_header;
567             char *psz_dup;
568             int i;
569
570             vlc_gc_decref( p_media->vod.p_item );
571             p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
572                 p_cfg->psz_name );
573
574             if( p_cfg->psz_output )
575             {
576                 if( asprintf( &psz_output, "%s:description", p_cfg->psz_output )  == -1 )
577                     psz_output = NULL;
578             }
579             else
580                 psz_output = strdup( "#description" );
581
582             if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
583             {
584                 input_item_AddOption( p_media->vod.p_item, psz_dup, VLC_INPUT_OPTION_TRUSTED );
585                 free( psz_dup );
586             }
587             free( psz_output );
588
589             for( i = 0; i < p_cfg->i_option; i++ )
590                 input_item_AddOption( p_media->vod.p_item,
591                                       p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
592
593             if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
594                 psz_header = NULL;
595
596             p_input = input_Create( p_vlm->p_vod, p_media->vod.p_item, psz_header, NULL );
597             if( p_input )
598             {
599                 vlc_sem_t sem_preparse;
600                 vlc_sem_init( &sem_preparse, 0 );
601                 var_AddCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
602
603                 if( !input_Start( p_input ) )
604                 {
605                     while( !p_input->b_dead && ( !p_cfg->vod.psz_mux || !input_item_IsPreparsed( p_media->vod.p_item ) ) )
606                         vlc_sem_wait( &sem_preparse );
607                 }
608
609                 var_DelCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
610                 vlc_sem_destroy( &sem_preparse );
611
612                 input_Stop( p_input, false );
613                 vlc_thread_join( p_input );
614                 vlc_object_release( p_input );
615             }
616             free( psz_header );
617
618             if( p_cfg->vod.psz_mux )
619             {
620                 input_item_t item;
621                 es_format_t es, *p_es = &es;
622                 union { char text[5]; uint32_t value; } fourcc;
623
624                 sprintf( fourcc.text, "%4.4s", p_cfg->vod.psz_mux );
625                 fourcc.text[0] = tolower(fourcc.text[0]);
626                 fourcc.text[1] = tolower(fourcc.text[1]);
627                 fourcc.text[2] = tolower(fourcc.text[2]);
628                 fourcc.text[3] = tolower(fourcc.text[3]);
629
630                 /* XXX: Don't do it that way, but properly use a new input item ref. */
631                 item = *p_media->vod.p_item;
632                 item.i_es = 1;
633                 item.es = &p_es;
634                 es_format_Init( &es, VIDEO_ES, fourcc.value );
635
636                 p_media->vod.p_media =
637                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
638             }
639             else
640             {
641                 p_media->vod.p_media =
642                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
643             }
644         }
645     }
646     else
647     {
648         /* TODO start media if needed */
649     }
650
651     /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
652
653     vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
654     return VLC_SUCCESS;
655 }
656 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
657 {
658     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
659
660     /* */
661     if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
662         return VLC_EGENERIC;
663     if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
664         return VLC_EGENERIC;
665
666     if( 0 )
667     {
668         /* TODO check what are the changes being done (stop instance if needed) */
669     }
670
671     vlm_media_Clean( &p_media->cfg );
672     vlm_media_Copy( &p_media->cfg, p_cfg );
673
674     return vlm_OnMediaUpdate( p_vlm, p_media );
675 }
676
677 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
678 {
679     vlm_media_sys_t *p_media;
680
681     if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
682     {
683         msg_Err( p_vlm, "invalid media description" );
684         return VLC_EGENERIC;
685     }
686     /* Check if we need to load the VOD server */
687     if( p_cfg->b_vod && !p_vlm->i_vod )
688     {
689         p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
690                                           VLC_OBJECT_GENERIC, "vod server" );
691         vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
692         p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
693         if( !p_vlm->p_vod->p_module )
694         {
695             msg_Err( p_vlm, "cannot find vod server" );
696             vlc_object_release( p_vlm->p_vod );
697             p_vlm->p_vod = NULL;
698             return VLC_EGENERIC;
699         }
700
701         p_vlm->p_vod->p_data = p_vlm;
702         p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
703     }
704
705     p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
706     if( !p_media )
707         return VLC_ENOMEM;
708
709     if( p_cfg->b_vod )
710         p_vlm->i_vod++;
711
712     vlm_media_Copy( &p_media->cfg, p_cfg );
713     p_media->cfg.id = p_vlm->i_id++;
714     /* FIXME do we do something here if enabled is true ? */
715
716     p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
717
718     p_media->vod.p_media = NULL;
719     TAB_INIT( p_media->i_instance, p_media->instance );
720
721     /* */
722     TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
723
724     if( p_id )
725         *p_id = p_media->cfg.id;
726
727     /* */
728     vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
729     return vlm_OnMediaUpdate( p_vlm, p_media );
730 }
731
732 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
733 {
734     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
735
736     if( !p_media )
737         return VLC_EGENERIC;
738
739     while( p_media->i_instance > 0 )
740         vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
741
742     if( p_media->cfg.b_vod )
743     {
744         p_media->cfg.b_enabled = false;
745         vlm_OnMediaUpdate( p_vlm, p_media );
746         p_vlm->i_vod--;
747     }
748
749     /* */
750     vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );
751
752     vlm_media_Clean( &p_media->cfg );
753
754     vlc_gc_decref( p_media->vod.p_item );
755
756     if( p_media->vod.p_media )
757         p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
758
759     TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
760     free( p_media );
761
762     /* Check if we need to unload the VOD server */
763     if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
764     {
765         module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
766         vlc_object_release( p_vlm->p_vod );
767         p_vlm->p_vod = NULL;
768     }
769
770     return VLC_SUCCESS;
771 }
772
773 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
774 {
775     vlm_media_t **pp_dsc;
776     int                     i_dsc;
777     int i;
778
779     TAB_INIT( i_dsc, pp_dsc );
780     for( i = 0; i < p_vlm->i_media; i++ )
781     {
782         vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
783         TAB_APPEND( i_dsc, pp_dsc, p_dsc );
784     }
785
786     *ppp_dsc = pp_dsc;
787     *pi_dsc = i_dsc;
788
789     return VLC_SUCCESS;
790 }
791 static int vlm_ControlMediaClear( vlm_t *p_vlm )
792 {
793     while( p_vlm->i_media > 0 )
794         vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
795
796     return VLC_SUCCESS;
797 }
798 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
799 {
800     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
801     if( !p_media )
802         return VLC_EGENERIC;
803
804     *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
805     return VLC_SUCCESS;
806 }
807 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
808 {
809     vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
810     if( !p_media )
811         return VLC_EGENERIC;
812
813     *p_id = p_media->cfg.id;
814     return VLC_SUCCESS;
815 }
816
817 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
818 {
819     int i;
820
821     for( i = 0; i < p_media->i_instance; i++ )
822     {
823         const char *psz = p_media->instance[i]->psz_name;
824         if( ( psz == NULL && psz_id == NULL ) ||
825             ( psz && psz_id && !strcmp( psz, psz_id ) ) )
826             return p_media->instance[i];
827     }
828     return NULL;
829 }
830 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
831 {
832     vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
833     if( !p_instance )
834         return NULL;
835
836     p_instance->psz_name = NULL;
837     if( psz_name )
838         p_instance->psz_name = strdup( psz_name );
839
840     p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
841
842     p_instance->i_index = 0;
843     p_instance->b_sout_keep = false;
844     p_instance->p_input = NULL;
845     p_instance->p_input_resource = NULL;
846
847     return p_instance;
848 }
849 static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, vlm_media_sys_t *p_media )
850 {
851     input_thread_t *p_input = p_instance->p_input;
852     if( p_input )
853     {
854         input_resource_t *p_resource;
855
856         input_Stop( p_input, true );
857         vlc_thread_join( p_input );
858
859         p_resource = input_DetachResource( p_input );
860         input_resource_Delete( p_resource );
861
862         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
863         vlc_object_release( p_input );
864
865         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
866     }
867     if( p_instance->p_input_resource )
868         input_resource_Delete( p_instance->p_input_resource );
869
870     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
871     vlc_gc_decref( p_instance->p_item );
872     free( p_instance->psz_name );
873     free( p_instance );
874 }
875
876
877 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
878 {
879     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
880     vlm_media_instance_sys_t *p_instance;
881     char *psz_log;
882
883     if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
884         return VLC_EGENERIC;
885
886     /* TODO support multiple input for VOD with sout-keep ? */
887
888     if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
889         return VLC_EGENERIC;
890
891     if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
892         return VLC_EGENERIC;
893
894     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
895     if( !p_instance )
896     {
897         vlm_media_t *p_cfg = &p_media->cfg;
898         int i;
899
900         p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
901         if( !p_instance )
902             return VLC_ENOMEM;
903
904         if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
905         {
906             char *psz_buffer;
907             if( asprintf( &psz_buffer, "sout=%s%s%s",
908                       p_cfg->psz_output ? p_cfg->psz_output : "",
909                       (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
910                       psz_vod_output ? psz_vod_output : "" ) != -1 )
911             {
912                 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
913                 free( psz_buffer );
914             }
915         }
916
917         for( i = 0; i < p_cfg->i_option; i++ )
918         {
919             if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
920                 p_instance->b_sout_keep = true;
921             else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
922                 p_instance->b_sout_keep = false;
923             else
924                 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
925         }
926         TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
927     }
928
929     /* Stop old instance */
930     input_thread_t *p_input = p_instance->p_input;
931     if( p_input )
932     {
933         if( p_instance->i_index == i_input_index &&
934             !p_input->b_eof && !p_input->b_error )
935         {
936             if( var_GetInteger( p_input, "state" ) == PAUSE_S )
937                 var_SetInteger( p_input, "state",  PLAYING_S );
938             return VLC_SUCCESS;
939         }
940
941         input_Stop( p_input, !p_input->b_eof && !p_input->b_error );
942         vlc_thread_join( p_input );
943
944         p_instance->p_input_resource = input_DetachResource( p_input );
945
946         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
947         vlc_object_release( p_input );
948
949         if( !p_instance->b_sout_keep )
950             input_resource_TerminateSout( p_instance->p_input_resource );
951         input_resource_TerminateVout( p_instance->p_input_resource );
952
953         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
954     }
955
956     /* Start new one */
957     p_instance->i_index = i_input_index;
958     input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
959
960     if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
961     {
962         vlc_object_t *p_parent = p_media->cfg.b_vod ?
963                                      VLC_OBJECT(p_vlm->p_vod) :
964                                      VLC_OBJECT(p_vlm->p_libvlc);
965         p_instance->p_input = input_Create( p_parent, p_instance->p_item,
966                                             psz_log, p_instance->p_input_resource );
967         if( p_instance->p_input )
968         {
969             var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
970             if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
971             {
972                 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
973                 vlc_object_release( p_instance->p_input );
974                 p_instance->p_input = NULL;
975             }
976         }
977         p_instance->p_input_resource = NULL;
978
979         if( !p_instance->p_input )
980         {
981             vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
982         }
983         else
984         {
985             vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
986         }
987         free( psz_log );
988     }
989
990     return VLC_SUCCESS;
991 }
992
993 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
994 {
995     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
996     vlm_media_instance_sys_t *p_instance;
997
998     if( !p_media )
999         return VLC_EGENERIC;
1000
1001     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1002     if( !p_instance )
1003         return VLC_EGENERIC;
1004
1005     vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
1006
1007     return VLC_SUCCESS;
1008 }
1009 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
1010 {
1011     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1012     vlm_media_instance_sys_t *p_instance;
1013     int i_state;
1014
1015     if( !p_media )
1016         return VLC_EGENERIC;
1017
1018     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1019     if( !p_instance || !p_instance->p_input )
1020         return VLC_EGENERIC;
1021
1022     /* Toggle pause state */
1023     i_state = var_GetInteger( p_instance->p_input, "state" );
1024     if( i_state == PAUSE_S )
1025         var_SetInteger( p_instance->p_input, "state", PLAYING_S );
1026     else if( i_state == PLAYING_S )
1027         var_SetInteger( p_instance->p_input, "state", PAUSE_S );
1028     return VLC_SUCCESS;
1029 }
1030 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
1031 {
1032     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1033     vlm_media_instance_sys_t *p_instance;
1034
1035     if( !p_media )
1036         return VLC_EGENERIC;
1037
1038     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1039     if( !p_instance || !p_instance->p_input )
1040         return VLC_EGENERIC;
1041
1042     if( pi_time )
1043         *pi_time = var_GetTime( p_instance->p_input, "time" );
1044     if( pd_position )
1045         *pd_position = var_GetFloat( p_instance->p_input, "position" );
1046     return VLC_SUCCESS;
1047 }
1048 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1049 {
1050     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1051     vlm_media_instance_sys_t *p_instance;
1052
1053     if( !p_media )
1054         return VLC_EGENERIC;
1055
1056     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1057     if( !p_instance || !p_instance->p_input )
1058         return VLC_EGENERIC;
1059
1060     if( i_time >= 0 )
1061         return var_SetTime( p_instance->p_input, "time", i_time );
1062     else if( d_position >= 0 && d_position <= 100 )
1063         return var_SetFloat( p_instance->p_input, "position", d_position );
1064     return VLC_EGENERIC;
1065 }
1066
1067 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1068 {
1069     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1070     vlm_media_instance_t **pp_idsc;
1071     int                              i_idsc;
1072     int i;
1073
1074     if( !p_media )
1075         return VLC_EGENERIC;
1076
1077     TAB_INIT( i_idsc, pp_idsc );
1078     for( i = 0; i < p_media->i_instance; i++ )
1079     {
1080         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1081         vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1082
1083         if( p_instance->psz_name )
1084             p_idsc->psz_name = strdup( p_instance->psz_name );
1085         if( p_instance->p_input )
1086         {
1087             p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1088             p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1089             p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1090             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1091                 p_idsc->b_paused = true;
1092             p_idsc->i_rate = INPUT_RATE_DEFAULT
1093                              / var_GetFloat( p_instance->p_input, "rate" );
1094         }
1095
1096         TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1097     }
1098     *ppp_idsc = pp_idsc;
1099     *pi_instance = i_idsc;
1100     return VLC_SUCCESS;
1101 }
1102
1103 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1104 {
1105     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1106
1107     if( !p_media )
1108         return VLC_EGENERIC;
1109
1110     while( p_media->i_instance > 0 )
1111         vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1112
1113     return VLC_SUCCESS;
1114 }
1115
1116 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1117 {
1118     while( p_vlm->i_schedule > 0 )
1119         vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1120
1121     return VLC_SUCCESS;
1122 }
1123
1124 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1125 {
1126     vlm_media_t *p_dsc;
1127     vlm_media_t **pp_dsc;
1128     vlm_media_t ***ppp_dsc;
1129     vlm_media_instance_t ***ppp_idsc;
1130     const char *psz_id;
1131     const char *psz_vod;
1132     int64_t *p_id;
1133     int64_t id;
1134     int i_int;
1135     int *pi_int;
1136
1137     int64_t *pi_i64;
1138     int64_t i_i64;
1139     double *pd_double;
1140     double d_double;
1141
1142     switch( i_query )
1143     {
1144     /* Media control */
1145     case VLM_GET_MEDIAS:
1146         ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1147         pi_int = (int *)va_arg( args, int * );
1148         return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1149
1150     case VLM_CLEAR_MEDIAS:
1151         return vlm_ControlMediaClear( p_vlm );
1152
1153     case VLM_CHANGE_MEDIA:
1154         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1155         return vlm_ControlMediaChange( p_vlm, p_dsc );
1156
1157     case VLM_ADD_MEDIA:
1158         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1159         p_id = (int64_t*)va_arg( args, int64_t * );
1160         return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1161
1162     case VLM_DEL_MEDIA:
1163         id = (int64_t)va_arg( args, int64_t );
1164         return vlm_ControlMediaDel( p_vlm, id );
1165
1166     case VLM_GET_MEDIA:
1167         id = (int64_t)va_arg( args, int64_t );
1168         pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1169         return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1170
1171     case VLM_GET_MEDIA_ID:
1172         psz_id = (const char*)va_arg( args, const char * );
1173         p_id = (int64_t*)va_arg( args, int64_t * );
1174         return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1175
1176
1177     /* Media instance control */
1178     case VLM_GET_MEDIA_INSTANCES:
1179         id = (int64_t)va_arg( args, int64_t );
1180         ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1181         pi_int = (int *)va_arg( args, int *);
1182         return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1183
1184     case VLM_CLEAR_MEDIA_INSTANCES:
1185         id = (int64_t)va_arg( args, int64_t );
1186         return vlm_ControlMediaInstanceClear( p_vlm, id );
1187
1188
1189     case VLM_START_MEDIA_BROADCAST_INSTANCE:
1190         id = (int64_t)va_arg( args, int64_t );
1191         psz_id = (const char*)va_arg( args, const char* );
1192         i_int = (int)va_arg( args, int );
1193         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1194
1195     case VLM_START_MEDIA_VOD_INSTANCE:
1196         id = (int64_t)va_arg( args, int64_t );
1197         psz_id = (const char*)va_arg( args, const char* );
1198         i_int = (int)va_arg( args, int );
1199         psz_vod = (const char*)va_arg( args, const char* );
1200         if( !psz_vod )
1201             return VLC_EGENERIC;
1202         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1203
1204     case VLM_STOP_MEDIA_INSTANCE:
1205         id = (int64_t)va_arg( args, int64_t );
1206         psz_id = (const char*)va_arg( args, const char* );
1207         return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1208
1209     case VLM_PAUSE_MEDIA_INSTANCE:
1210         id = (int64_t)va_arg( args, int64_t );
1211         psz_id = (const char*)va_arg( args, const char* );
1212         return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1213
1214     case VLM_GET_MEDIA_INSTANCE_TIME:
1215         id = (int64_t)va_arg( args, int64_t );
1216         psz_id = (const char*)va_arg( args, const char* );
1217         pi_i64 = (int64_t*)va_arg( args, int64_t * );
1218         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1219     case VLM_GET_MEDIA_INSTANCE_POSITION:
1220         id = (int64_t)va_arg( args, int64_t );
1221         psz_id = (const char*)va_arg( args, const char* );
1222         pd_double = (double*)va_arg( args, double* );
1223         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1224
1225     case VLM_SET_MEDIA_INSTANCE_TIME:
1226         id = (int64_t)va_arg( args, int64_t );
1227         psz_id = (const char*)va_arg( args, const char* );
1228         i_i64 = (int64_t)va_arg( args, int64_t);
1229         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1230     case VLM_SET_MEDIA_INSTANCE_POSITION:
1231         id = (int64_t)va_arg( args, int64_t );
1232         psz_id = (const char*)va_arg( args, const char* );
1233         d_double = (double)va_arg( args, double );
1234         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1235
1236     case VLM_CLEAR_SCHEDULES:
1237         return vlm_ControlScheduleClear( p_vlm );
1238
1239     default:
1240         msg_Err( p_vlm, "unknown VLM query" );
1241         return VLC_EGENERIC;
1242     }
1243 }
1244
1245 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1246 {
1247     va_list args;
1248     int     i_result;
1249
1250     va_start( args, i_query );
1251     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1252     va_end( args );
1253
1254     return i_result;
1255 }
1256
1257 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1258 {
1259     va_list args;
1260     int     i_result;
1261
1262     va_start( args, i_query );
1263
1264     vlc_mutex_lock( &p_vlm->lock );
1265     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1266     vlc_mutex_unlock( &p_vlm->lock );
1267
1268     va_end( args );
1269
1270     return i_result;
1271 }
1272