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