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