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