650d87f6509ee809cc6db9a545a67e4717956883
[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_manage );
109         vlc_cond_signal( &p_vlm->wait_manage );
110         vlc_mutex_unlock( &p_vlm->lock_manage );
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_manage );
150     vlc_cond_init_daytime( &p_vlm->wait_manage );
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_manage );
162         vlc_mutex_destroy( &p_vlm->lock_manage );
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_manage );
222     vlc_cond_signal( &p_vlm->wait_manage );
223     vlc_mutex_unlock( &p_vlm->lock_manage );
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_manage );
231     vlc_mutex_destroy( &p_vlm->lock_manage );
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_manage );
377         if( i_nextschedule )
378             vlc_cond_timedwait( &vlm->wait_manage, &vlm->lock_manage, i_nextschedule );
379         else
380             vlc_cond_wait( &vlm->wait_manage, &vlm->lock_manage );
381         vlc_mutex_unlock( &vlm->lock_manage );
382
383         vlc_mutex_lock( &vlm->lock );
384
385         /* destroy the inputs that wants to die, and launch the next input */
386         for( i = 0; i < vlm->i_media; i++ )
387         {
388             vlm_media_sys_t *p_media = vlm->media[i];
389
390             for( j = 0; j < p_media->i_instance; )
391             {
392                 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
393
394                 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
395                 {
396                     int i_new_input_index;
397
398                     /* */
399                     i_new_input_index = p_instance->i_index + 1;
400                     if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
401                         i_new_input_index = 0;
402
403                     /* FIXME implement multiple input with VOD */
404                     if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
405                         vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
406                     else
407                         vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
408
409                     j = 0;
410                 }
411                 else
412                 {
413                     j++;
414                 }
415             }
416         }
417
418         /* scheduling */
419         i_time = vlm_Date();
420         i_nextschedule = 0;
421
422         for( i = 0; i < vlm->i_schedule; i++ )
423         {
424             mtime_t i_real_date = vlm->schedule[i]->i_date;
425
426             if( vlm->schedule[i]->b_enabled == true )
427             {
428                 if( vlm->schedule[i]->i_date == 0 ) // now !
429                 {
430                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
431                     i_real_date = i_time;
432                 }
433                 else if( vlm->schedule[i]->i_period != 0 )
434                 {
435                     int j = 0;
436                     while( vlm->schedule[i]->i_date + j *
437                            vlm->schedule[i]->i_period <= i_lastcheck &&
438                            ( vlm->schedule[i]->i_repeat > j ||
439                              vlm->schedule[i]->i_repeat == -1 ) )
440                     {
441                         j++;
442                     }
443
444                     i_real_date = vlm->schedule[i]->i_date + j *
445                         vlm->schedule[i]->i_period;
446                 }
447
448                 if( i_real_date <= i_time )
449                 {
450                     if( i_real_date > i_lastcheck )
451                     {
452                         for( j = 0; j < vlm->schedule[i]->i_command; j++ )
453                         {
454                             TAB_APPEND( i_scheduled_commands,
455                                         ppsz_scheduled_commands,
456                                         strdup(vlm->schedule[i]->command[j] ) );
457                         }
458                     }
459                 }
460                 else
461                 {
462                     i_nextschedule = i_real_date;
463                 }
464             }
465         }
466
467         while( i_scheduled_commands )
468         {
469             vlm_message_t *message = NULL;
470             char *psz_command = ppsz_scheduled_commands[0];
471             ExecuteCommand( vlm, psz_command,&message );
472
473             /* for now, drop the message */
474             vlm_MessageDelete( message );
475             TAB_REMOVE( i_scheduled_commands,
476                         ppsz_scheduled_commands,
477                         psz_command );
478             free( psz_command );
479         }
480
481         i_lastcheck = i_time;
482
483         vlc_mutex_unlock( &vlm->lock );
484     }
485
486     vlc_restorecancel (canc);
487     return NULL;
488 }
489
490 /* New API
491  */
492 /*
493 typedef struct
494 {
495     struct
496     {
497         int i_connection_count;
498         int i_connection_active;
499     } vod;
500     struct
501     {
502         int        i_count;
503         bool b_playing;
504         int        i_playing_index;
505     } broadcast;
506
507 } vlm_media_status_t;
508 */
509
510 /* */
511 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
512 {
513     int i;
514
515     for( i = 0; i < p_vlm->i_media; i++ )
516     {
517         if( p_vlm->media[i]->cfg.id == id )
518             return p_vlm->media[i];
519     }
520     return NULL;
521 }
522 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
523 {
524     int i;
525
526     for( i = 0; i < p_vlm->i_media; i++ )
527     {
528         if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
529             return p_vlm->media[i];
530     }
531     return NULL;
532 }
533 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
534 {
535     int i;
536
537     if( !p_cfg || !p_cfg->psz_name ||
538         !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
539         return VLC_EGENERIC;
540
541     for( i = 0; i < p_vlm->i_media; i++ )
542     {
543         if( p_vlm->media[i]->cfg.id == p_cfg->id )
544             continue;
545         if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
546             return VLC_EGENERIC;
547     }
548     return VLC_SUCCESS;
549 }
550
551
552 /* Called after a media description is changed/added */
553 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
554 {
555     vlm_media_t *p_cfg = &p_media->cfg;
556     /* Check if we need to create/delete a vod media */
557     if( p_cfg->b_vod )
558     {
559         if( !p_cfg->b_enabled && p_media->vod.p_media )
560         {
561             p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
562             p_media->vod.p_media = NULL;
563         }
564         else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
565         {
566             /* Pre-parse the input */
567             input_thread_t *p_input;
568             char *psz_output;
569             char *psz_header;
570             char *psz_dup;
571             int i;
572
573             vlc_gc_decref( p_media->vod.p_item );
574             p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
575                 p_cfg->psz_name );
576
577             if( p_cfg->psz_output )
578             {
579                 if( asprintf( &psz_output, "%s:description", p_cfg->psz_output )  == -1 )
580                     psz_output = NULL;
581             }
582             else
583                 psz_output = strdup( "#description" );
584
585             if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
586             {
587                 input_item_AddOption( p_media->vod.p_item, psz_dup, VLC_INPUT_OPTION_TRUSTED );
588                 free( psz_dup );
589             }
590             free( psz_output );
591
592             for( i = 0; i < p_cfg->i_option; i++ )
593                 input_item_AddOption( p_media->vod.p_item,
594                                       p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
595
596             if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
597                 psz_header = NULL;
598
599             p_input = input_Create( p_vlm->p_vod, p_media->vod.p_item, psz_header, NULL );
600             if( p_input )
601             {
602                 vlc_sem_t sem_preparse;
603                 vlc_sem_init( &sem_preparse, 0 );
604                 var_AddCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
605
606                 if( !input_Start( p_input ) )
607                 {
608                     while( !p_input->b_dead && ( !p_cfg->vod.psz_mux || !input_item_IsPreparsed( p_media->vod.p_item ) ) )
609                         vlc_sem_wait( &sem_preparse );
610                 }
611
612                 var_DelCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
613                 vlc_sem_destroy( &sem_preparse );
614
615                 input_Stop( p_input, false );
616                 vlc_thread_join( p_input );
617                 vlc_object_release( p_input );
618             }
619             free( psz_header );
620
621             if( p_cfg->vod.psz_mux )
622             {
623                 input_item_t item;
624                 es_format_t es, *p_es = &es;
625                 union { char text[5]; uint32_t value; } fourcc;
626
627                 sprintf( fourcc.text, "%4.4s", p_cfg->vod.psz_mux );
628                 fourcc.text[0] = tolower(fourcc.text[0]);
629                 fourcc.text[1] = tolower(fourcc.text[1]);
630                 fourcc.text[2] = tolower(fourcc.text[2]);
631                 fourcc.text[3] = tolower(fourcc.text[3]);
632
633                 /* XXX: Don't do it that way, but properly use a new input item ref. */
634                 item = *p_media->vod.p_item;
635                 item.i_es = 1;
636                 item.es = &p_es;
637                 es_format_Init( &es, VIDEO_ES, fourcc.value );
638
639                 p_media->vod.p_media =
640                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
641             }
642             else
643             {
644                 p_media->vod.p_media =
645                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
646             }
647         }
648     }
649     else
650     {
651         /* TODO start media if needed */
652     }
653
654     /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
655
656     vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
657     return VLC_SUCCESS;
658 }
659 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
660 {
661     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
662
663     /* */
664     if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
665         return VLC_EGENERIC;
666     if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
667         return VLC_EGENERIC;
668
669     if( 0 )
670     {
671         /* TODO check what are the changes being done (stop instance if needed) */
672     }
673
674     vlm_media_Clean( &p_media->cfg );
675     vlm_media_Copy( &p_media->cfg, p_cfg );
676
677     return vlm_OnMediaUpdate( p_vlm, p_media );
678 }
679
680 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
681 {
682     vlm_media_sys_t *p_media;
683
684     if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
685     {
686         msg_Err( p_vlm, "invalid media description" );
687         return VLC_EGENERIC;
688     }
689     /* Check if we need to load the VOD server */
690     if( p_cfg->b_vod && !p_vlm->i_vod )
691     {
692         p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
693                                           VLC_OBJECT_GENERIC, "vod server" );
694         vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
695         p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
696         if( !p_vlm->p_vod->p_module )
697         {
698             msg_Err( p_vlm, "cannot find vod server" );
699             vlc_object_release( p_vlm->p_vod );
700             p_vlm->p_vod = NULL;
701             return VLC_EGENERIC;
702         }
703
704         p_vlm->p_vod->p_data = p_vlm;
705         p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
706     }
707
708     p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
709     if( !p_media )
710         return VLC_ENOMEM;
711
712     if( p_cfg->b_vod )
713         p_vlm->i_vod++;
714
715     vlm_media_Copy( &p_media->cfg, p_cfg );
716     p_media->cfg.id = p_vlm->i_id++;
717     /* FIXME do we do something here if enabled is true ? */
718
719     p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
720
721     p_media->vod.p_media = NULL;
722     TAB_INIT( p_media->i_instance, p_media->instance );
723
724     /* */
725     TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
726
727     if( p_id )
728         *p_id = p_media->cfg.id;
729
730     /* */
731     vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
732     return vlm_OnMediaUpdate( p_vlm, p_media );
733 }
734
735 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
736 {
737     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
738
739     if( !p_media )
740         return VLC_EGENERIC;
741
742     while( p_media->i_instance > 0 )
743         vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
744
745     if( p_media->cfg.b_vod )
746     {
747         p_media->cfg.b_enabled = false;
748         vlm_OnMediaUpdate( p_vlm, p_media );
749         p_vlm->i_vod--;
750     }
751
752     /* */
753     vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );
754
755     vlm_media_Clean( &p_media->cfg );
756
757     vlc_gc_decref( p_media->vod.p_item );
758
759     if( p_media->vod.p_media )
760         p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
761
762     TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
763     free( p_media );
764
765     /* Check if we need to unload the VOD server */
766     if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
767     {
768         module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
769         vlc_object_release( p_vlm->p_vod );
770         p_vlm->p_vod = NULL;
771     }
772
773     return VLC_SUCCESS;
774 }
775
776 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
777 {
778     vlm_media_t **pp_dsc;
779     int                     i_dsc;
780     int i;
781
782     TAB_INIT( i_dsc, pp_dsc );
783     for( i = 0; i < p_vlm->i_media; i++ )
784     {
785         vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
786         TAB_APPEND( i_dsc, pp_dsc, p_dsc );
787     }
788
789     *ppp_dsc = pp_dsc;
790     *pi_dsc = i_dsc;
791
792     return VLC_SUCCESS;
793 }
794 static int vlm_ControlMediaClear( vlm_t *p_vlm )
795 {
796     while( p_vlm->i_media > 0 )
797         vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
798
799     return VLC_SUCCESS;
800 }
801 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
802 {
803     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
804     if( !p_media )
805         return VLC_EGENERIC;
806
807     *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
808     return VLC_SUCCESS;
809 }
810 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
811 {
812     vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
813     if( !p_media )
814         return VLC_EGENERIC;
815
816     *p_id = p_media->cfg.id;
817     return VLC_SUCCESS;
818 }
819
820 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
821 {
822     int i;
823
824     for( i = 0; i < p_media->i_instance; i++ )
825     {
826         const char *psz = p_media->instance[i]->psz_name;
827         if( ( psz == NULL && psz_id == NULL ) ||
828             ( psz && psz_id && !strcmp( psz, psz_id ) ) )
829             return p_media->instance[i];
830     }
831     return NULL;
832 }
833 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
834 {
835     vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
836     if( !p_instance )
837         return NULL;
838
839     p_instance->psz_name = NULL;
840     if( psz_name )
841         p_instance->psz_name = strdup( psz_name );
842
843     p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
844
845     p_instance->i_index = 0;
846     p_instance->b_sout_keep = false;
847     p_instance->p_input = NULL;
848     p_instance->p_input_resource = NULL;
849
850     return p_instance;
851 }
852 static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, vlm_media_sys_t *p_media )
853 {
854     input_thread_t *p_input = p_instance->p_input;
855     if( p_input )
856     {
857         input_resource_t *p_resource;
858
859         input_Stop( p_input, true );
860         vlc_thread_join( p_input );
861
862         p_resource = input_DetachResource( p_input );
863         input_resource_Delete( p_resource );
864
865         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
866         vlc_object_release( p_input );
867
868         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
869     }
870     if( p_instance->p_input_resource )
871         input_resource_Delete( p_instance->p_input_resource );
872
873     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
874     vlc_gc_decref( p_instance->p_item );
875     free( p_instance->psz_name );
876     free( p_instance );
877 }
878
879
880 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
881 {
882     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
883     vlm_media_instance_sys_t *p_instance;
884     char *psz_log;
885
886     if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
887         return VLC_EGENERIC;
888
889     /* TODO support multiple input for VOD with sout-keep ? */
890
891     if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
892         return VLC_EGENERIC;
893
894     if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
895         return VLC_EGENERIC;
896
897     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
898     if( !p_instance )
899     {
900         vlm_media_t *p_cfg = &p_media->cfg;
901         int i;
902
903         p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
904         if( !p_instance )
905             return VLC_ENOMEM;
906
907         if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
908         {
909             char *psz_buffer;
910             if( asprintf( &psz_buffer, "sout=%s%s%s",
911                       p_cfg->psz_output ? p_cfg->psz_output : "",
912                       (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
913                       psz_vod_output ? psz_vod_output : "" ) != -1 )
914             {
915                 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
916                 free( psz_buffer );
917             }
918         }
919
920         for( i = 0; i < p_cfg->i_option; i++ )
921         {
922             if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
923                 p_instance->b_sout_keep = true;
924             else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
925                 p_instance->b_sout_keep = false;
926             else
927                 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
928         }
929         TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
930     }
931
932     /* Stop old instance */
933     input_thread_t *p_input = p_instance->p_input;
934     if( p_input )
935     {
936         if( p_instance->i_index == i_input_index &&
937             !p_input->b_eof && !p_input->b_error )
938         {
939             if( var_GetInteger( p_input, "state" ) == PAUSE_S )
940                 var_SetInteger( p_input, "state",  PLAYING_S );
941             return VLC_SUCCESS;
942         }
943
944         input_Stop( p_input, !p_input->b_eof && !p_input->b_error );
945         vlc_thread_join( p_input );
946
947         p_instance->p_input_resource = input_DetachResource( p_input );
948
949         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
950         vlc_object_release( p_input );
951
952         if( !p_instance->b_sout_keep )
953             input_resource_TerminateSout( p_instance->p_input_resource );
954         input_resource_TerminateVout( p_instance->p_input_resource );
955
956         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
957     }
958
959     /* Start new one */
960     p_instance->i_index = i_input_index;
961     input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
962
963     if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
964     {
965         vlc_object_t *p_parent = p_media->cfg.b_vod ?
966                                      VLC_OBJECT(p_vlm->p_vod) :
967                                      VLC_OBJECT(p_vlm->p_libvlc);
968         p_instance->p_input = input_Create( p_parent, p_instance->p_item,
969                                             psz_log, p_instance->p_input_resource );
970         if( p_instance->p_input )
971         {
972             var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
973             if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
974             {
975                 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
976                 vlc_object_release( p_instance->p_input );
977                 p_instance->p_input = NULL;
978             }
979         }
980         p_instance->p_input_resource = NULL;
981
982         if( !p_instance->p_input )
983         {
984             vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
985         }
986         else
987         {
988             vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
989         }
990         free( psz_log );
991     }
992
993     return VLC_SUCCESS;
994 }
995
996 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
997 {
998     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
999     vlm_media_instance_sys_t *p_instance;
1000
1001     if( !p_media )
1002         return VLC_EGENERIC;
1003
1004     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1005     if( !p_instance )
1006         return VLC_EGENERIC;
1007
1008     vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
1009
1010     return VLC_SUCCESS;
1011 }
1012 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
1013 {
1014     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1015     vlm_media_instance_sys_t *p_instance;
1016     int i_state;
1017
1018     if( !p_media )
1019         return VLC_EGENERIC;
1020
1021     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1022     if( !p_instance || !p_instance->p_input )
1023         return VLC_EGENERIC;
1024
1025     /* Toggle pause state */
1026     i_state = var_GetInteger( p_instance->p_input, "state" );
1027     if( i_state == PAUSE_S )
1028         var_SetInteger( p_instance->p_input, "state", PLAYING_S );
1029     else if( i_state == PLAYING_S )
1030         var_SetInteger( p_instance->p_input, "state", PAUSE_S );
1031     return VLC_SUCCESS;
1032 }
1033 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
1034 {
1035     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1036     vlm_media_instance_sys_t *p_instance;
1037
1038     if( !p_media )
1039         return VLC_EGENERIC;
1040
1041     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1042     if( !p_instance || !p_instance->p_input )
1043         return VLC_EGENERIC;
1044
1045     if( pi_time )
1046         *pi_time = var_GetTime( p_instance->p_input, "time" );
1047     if( pd_position )
1048         *pd_position = var_GetFloat( p_instance->p_input, "position" );
1049     return VLC_SUCCESS;
1050 }
1051 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1052 {
1053     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1054     vlm_media_instance_sys_t *p_instance;
1055
1056     if( !p_media )
1057         return VLC_EGENERIC;
1058
1059     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1060     if( !p_instance || !p_instance->p_input )
1061         return VLC_EGENERIC;
1062
1063     if( i_time >= 0 )
1064         return var_SetTime( p_instance->p_input, "time", i_time );
1065     else if( d_position >= 0 && d_position <= 100 )
1066         return var_SetFloat( p_instance->p_input, "position", d_position );
1067     return VLC_EGENERIC;
1068 }
1069
1070 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1071 {
1072     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1073     vlm_media_instance_t **pp_idsc;
1074     int                              i_idsc;
1075     int i;
1076
1077     if( !p_media )
1078         return VLC_EGENERIC;
1079
1080     TAB_INIT( i_idsc, pp_idsc );
1081     for( i = 0; i < p_media->i_instance; i++ )
1082     {
1083         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1084         vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1085
1086         if( p_instance->psz_name )
1087             p_idsc->psz_name = strdup( p_instance->psz_name );
1088         if( p_instance->p_input )
1089         {
1090             p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1091             p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1092             p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1093             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1094                 p_idsc->b_paused = true;
1095             p_idsc->i_rate = INPUT_RATE_DEFAULT
1096                              / var_GetFloat( p_instance->p_input, "rate" );
1097         }
1098
1099         TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1100     }
1101     *ppp_idsc = pp_idsc;
1102     *pi_instance = i_idsc;
1103     return VLC_SUCCESS;
1104 }
1105
1106 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1107 {
1108     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1109
1110     if( !p_media )
1111         return VLC_EGENERIC;
1112
1113     while( p_media->i_instance > 0 )
1114         vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1115
1116     return VLC_SUCCESS;
1117 }
1118
1119 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1120 {
1121     while( p_vlm->i_schedule > 0 )
1122         vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1123
1124     return VLC_SUCCESS;
1125 }
1126
1127 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1128 {
1129     vlm_media_t *p_dsc;
1130     vlm_media_t **pp_dsc;
1131     vlm_media_t ***ppp_dsc;
1132     vlm_media_instance_t ***ppp_idsc;
1133     const char *psz_id;
1134     const char *psz_vod;
1135     int64_t *p_id;
1136     int64_t id;
1137     int i_int;
1138     int *pi_int;
1139
1140     int64_t *pi_i64;
1141     int64_t i_i64;
1142     double *pd_double;
1143     double d_double;
1144
1145     switch( i_query )
1146     {
1147     /* Media control */
1148     case VLM_GET_MEDIAS:
1149         ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1150         pi_int = (int *)va_arg( args, int * );
1151         return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1152
1153     case VLM_CLEAR_MEDIAS:
1154         return vlm_ControlMediaClear( p_vlm );
1155
1156     case VLM_CHANGE_MEDIA:
1157         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1158         return vlm_ControlMediaChange( p_vlm, p_dsc );
1159
1160     case VLM_ADD_MEDIA:
1161         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1162         p_id = (int64_t*)va_arg( args, int64_t * );
1163         return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1164
1165     case VLM_DEL_MEDIA:
1166         id = (int64_t)va_arg( args, int64_t );
1167         return vlm_ControlMediaDel( p_vlm, id );
1168
1169     case VLM_GET_MEDIA:
1170         id = (int64_t)va_arg( args, int64_t );
1171         pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1172         return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1173
1174     case VLM_GET_MEDIA_ID:
1175         psz_id = (const char*)va_arg( args, const char * );
1176         p_id = (int64_t*)va_arg( args, int64_t * );
1177         return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1178
1179
1180     /* Media instance control */
1181     case VLM_GET_MEDIA_INSTANCES:
1182         id = (int64_t)va_arg( args, int64_t );
1183         ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1184         pi_int = (int *)va_arg( args, int *);
1185         return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1186
1187     case VLM_CLEAR_MEDIA_INSTANCES:
1188         id = (int64_t)va_arg( args, int64_t );
1189         return vlm_ControlMediaInstanceClear( p_vlm, id );
1190
1191
1192     case VLM_START_MEDIA_BROADCAST_INSTANCE:
1193         id = (int64_t)va_arg( args, int64_t );
1194         psz_id = (const char*)va_arg( args, const char* );
1195         i_int = (int)va_arg( args, int );
1196         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1197
1198     case VLM_START_MEDIA_VOD_INSTANCE:
1199         id = (int64_t)va_arg( args, int64_t );
1200         psz_id = (const char*)va_arg( args, const char* );
1201         i_int = (int)va_arg( args, int );
1202         psz_vod = (const char*)va_arg( args, const char* );
1203         if( !psz_vod )
1204             return VLC_EGENERIC;
1205         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1206
1207     case VLM_STOP_MEDIA_INSTANCE:
1208         id = (int64_t)va_arg( args, int64_t );
1209         psz_id = (const char*)va_arg( args, const char* );
1210         return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1211
1212     case VLM_PAUSE_MEDIA_INSTANCE:
1213         id = (int64_t)va_arg( args, int64_t );
1214         psz_id = (const char*)va_arg( args, const char* );
1215         return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1216
1217     case VLM_GET_MEDIA_INSTANCE_TIME:
1218         id = (int64_t)va_arg( args, int64_t );
1219         psz_id = (const char*)va_arg( args, const char* );
1220         pi_i64 = (int64_t*)va_arg( args, int64_t * );
1221         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1222     case VLM_GET_MEDIA_INSTANCE_POSITION:
1223         id = (int64_t)va_arg( args, int64_t );
1224         psz_id = (const char*)va_arg( args, const char* );
1225         pd_double = (double*)va_arg( args, double* );
1226         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1227
1228     case VLM_SET_MEDIA_INSTANCE_TIME:
1229         id = (int64_t)va_arg( args, int64_t );
1230         psz_id = (const char*)va_arg( args, const char* );
1231         i_i64 = (int64_t)va_arg( args, int64_t);
1232         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1233     case VLM_SET_MEDIA_INSTANCE_POSITION:
1234         id = (int64_t)va_arg( args, int64_t );
1235         psz_id = (const char*)va_arg( args, const char* );
1236         d_double = (double)va_arg( args, double );
1237         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1238
1239     case VLM_CLEAR_SCHEDULES:
1240         return vlm_ControlScheduleClear( p_vlm );
1241
1242     default:
1243         msg_Err( p_vlm, "unknown VLM query" );
1244         return VLC_EGENERIC;
1245     }
1246 }
1247
1248 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1249 {
1250     va_list args;
1251     int     i_result;
1252
1253     va_start( args, i_query );
1254     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1255     va_end( args );
1256
1257     return i_result;
1258 }
1259
1260 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1261 {
1262     va_list args;
1263     int     i_result;
1264
1265     va_start( args, i_query );
1266
1267     vlc_mutex_lock( &p_vlm->lock );
1268     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1269     vlc_mutex_unlock( &p_vlm->lock );
1270
1271     va_end( args );
1272
1273     return i_result;
1274 }
1275