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