]> git.sesse.net Git - vlc/blob - src/input/vlm.c
Input access locking, part 3 (final).
[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 #include <vlc/vlc.h>
30
31 #include <stdio.h>
32 #include <ctype.h>                                              /* tolower() */
33 #include <assert.h>
34
35 #ifdef ENABLE_VLM
36
37
38 #ifdef HAVE_TIME_H
39 #   include <time.h>                                              /* ctime() */
40 #   include <sys/timeb.h>                                         /* ftime() */
41 #endif
42
43 #include <vlc_input.h>
44 #include "input_internal.h"
45 #include <vlc_stream.h>
46 #include <vlc_vlm.h>
47 #include "vlm_internal.h"
48 #include <vlc_vod.h>
49 #include <vlc_charset.h>
50 #include <vlc_sout.h>
51 #include "../stream_output/stream_output.h"
52
53 /*****************************************************************************
54  * Local prototypes.
55  *****************************************************************************/
56 /* */
57 static int vlm_ControlInternal( vlm_t *, int, ... );
58
59 /* */
60 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
61 static vlm_message_t *vlm_Help( vlm_t *, char * );
62
63 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
64
65 static char *Save( vlm_t * );
66 static int Load( vlm_t *, char * );
67
68 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
69
70 static int Manage( vlc_object_t * );
71
72 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
73 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched, const char *psz_name );
74 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
75                               const char *psz_value );
76
77 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
78
79 /* */
80 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
81
82 /*****************************************************************************
83  * vlm_New:
84  *****************************************************************************/
85 vlm_t *__vlm_New ( vlc_object_t *p_this )
86 {
87     vlc_value_t lockval;
88     vlm_t *p_vlm = NULL;
89     char *psz_vlmconf;
90
91     /* Avoid multiple creation */
92     if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
93         var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
94         return NULL;
95
96     vlc_mutex_lock( lockval.p_address );
97
98     p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
99     if( p_vlm )
100     {
101         vlc_object_yield( p_vlm );
102         vlc_mutex_unlock( lockval.p_address );
103         return p_vlm;
104     }
105
106     msg_Dbg( p_this, "creating VLM" );
107
108     p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
109     if( !p_vlm )
110     {
111         vlc_mutex_unlock( lockval.p_address );
112         return NULL;
113     }
114
115     vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
116     p_vlm->i_id = 1;
117     TAB_INIT( p_vlm->i_media, p_vlm->media );
118     TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
119     p_vlm->i_vod = 0;
120     p_vlm->p_vod = NULL;
121     vlc_object_yield( p_vlm );
122     vlc_object_attach( p_vlm, p_this->p_libvlc );
123
124     if( vlc_thread_create( p_vlm, "vlm thread",
125                            Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
126     {
127         vlc_mutex_destroy( &p_vlm->lock );
128         vlc_object_destroy( p_vlm );
129         return NULL;
130     }
131
132     /* Load our configuration file */
133     psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
134     if( psz_vlmconf && *psz_vlmconf )
135     {
136         vlm_message_t *p_message = NULL;
137         char *psz_buffer = NULL;
138
139         msg_Dbg( p_this, "loading VLM configuration" );
140         asprintf(&psz_buffer, "load %s", psz_vlmconf );
141         if( psz_buffer )
142         {
143             msg_Dbg( p_this, psz_buffer );
144             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
145                 msg_Warn( p_this, "error while loading the configuration file" );
146
147             vlm_MessageDelete(p_message);
148             free(psz_buffer);
149         }
150     }
151     free(psz_vlmconf);
152
153     vlc_mutex_unlock( lockval.p_address );
154
155     return p_vlm;
156 }
157
158 /*****************************************************************************
159  * vlm_Delete:
160  *****************************************************************************/
161 void vlm_Delete( vlm_t *p_vlm )
162 {
163     vlc_value_t lockval;
164
165     var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
166     vlc_mutex_lock( lockval.p_address );
167
168     vlc_object_release( p_vlm );
169
170     if( p_vlm->i_refcount > 0 )
171     {
172         vlc_mutex_unlock( lockval.p_address );
173         return;
174     }
175
176     vlc_object_kill( p_vlm );
177     vlc_thread_join( p_vlm );
178
179     vlc_object_detach( p_vlm );
180
181     vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
182     TAB_CLEAN( p_vlm->i_media, p_vlm->media );
183
184     vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
185     TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
186
187     vlc_mutex_destroy( &p_vlm->lock );
188
189     vlc_object_destroy( p_vlm );
190     vlc_mutex_unlock( lockval.p_address );
191 }
192
193 /*****************************************************************************
194  * vlm_ExecuteCommand:
195  *****************************************************************************/
196 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
197                         vlm_message_t **pp_message)
198 {
199     int i_result;
200
201     vlc_mutex_lock( &p_vlm->lock );
202     i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
203     vlc_mutex_unlock( &p_vlm->lock );
204
205     return i_result;
206 }
207
208
209 static const char quotes[] = "\"'";
210 /**
211  * FindCommandEnd: look for the end of a possibly quoted string
212  * @return NULL on mal-formatted string,
213  * pointer past the last character otherwise.
214  */
215 static const char *FindCommandEnd( const char *psz_sent )
216 {
217     char c, quote = 0;
218
219     while( (c = *psz_sent) != '\0' )
220     {
221         if( !quote )
222         {
223             if( strchr(quotes,c) )   // opening quote
224                 quote = c;
225             else if( isspace(c) )         // non-escaped space
226                 return psz_sent;
227             else if( c == '\\' )
228             {
229                 psz_sent++;         // skip escaped character
230                 if( *psz_sent == '\0' )
231                     return psz_sent;
232             }
233         }
234         else
235         {
236             if( c == quote )         // non-escaped matching quote
237                 quote = 0;
238             else if( (quote == '"') && (c == '\\') )
239             {
240                 psz_sent++;         // skip escaped character
241                 if (*psz_sent == '\0')
242                     return NULL;    // error, closing quote missing
243             }
244         }
245         psz_sent++;
246     }
247
248     // error (NULL) if we could not find a matching quote
249     return quote ? NULL : psz_sent;
250 }
251
252
253 /**
254  * Unescape a nul-terminated string.
255  * Note that in and out can be identical.
256  *
257  * @param out output buffer (at least <strlen (in) + 1> characters long)
258  * @param in nul-terminated string to be unescaped
259  *
260  * @return 0 on success, -1 on error.
261  */
262 static int Unescape( char *out, const char *in )
263 {
264     char c, quote = 0;
265
266     while( (c = *in++) != '\0' )
267     {
268         if( !quote )
269         {
270             if (strchr(quotes,c))   // opening quote
271             {
272                 quote = c;
273                 continue;
274             }
275             else if( c == '\\' )
276             {
277                 switch (c = *in++)
278                 {
279                     case '"':
280                     case '\'':
281                     case '\\':
282                         *out++ = c;
283                         continue;
284
285                     case '\0':
286                         *out = '\0';
287                         return 0;
288                 }
289                 if( isspace(c) )
290                 {
291                     *out++ = c;
292                     continue;
293                 }
294                 /* None of the special cases - copy the backslash */
295                 *out++ = '\\';
296             }
297         }
298         else
299         {
300             if( c == quote )         // non-escaped matching quote
301             {
302                 quote = 0;
303                 continue;
304             }
305             if( (quote == '"') && (c == '\\') )
306             {
307                 switch( c = *in++ )
308                 {
309                     case '"':
310                     case '\\':
311                         *out++ = c;
312                         continue;
313
314                     case '\0':   // should never happen
315                         *out = '\0';
316                         return -1;
317                 }
318                 /* None of the special cases - copy the backslash */
319                 *out++ = '\\';
320             }
321         }
322         *out++ = c;
323     }
324
325     *out = '\0';
326     return 0;
327 }
328
329
330 /*****************************************************************************
331  * ExecuteCommand: The main state machine
332  *****************************************************************************
333  * Execute a command which ends with '\0' (string)
334  *****************************************************************************/
335 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
336 {
337     *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
338     return VLC_EGENERIC;
339 }
340
341 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
342 {
343     int64_t id;
344
345     if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
346         return VLC_FALSE;
347     return VLC_TRUE;
348 }
349 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
350 {
351     if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
352         return VLC_FALSE;
353     return VLC_TRUE;
354 }
355
356 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
357 {
358     vlm_media_sys_t *p_media;
359     vlm_schedule_sys_t *p_schedule;
360
361     p_media = vlm_MediaSearch( p_vlm, psz_name );
362     p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
363
364     if( p_schedule != NULL )
365     {
366         vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
367     }
368     else if( p_media != NULL )
369     {
370         vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
371     }
372     else if( !strcmp(psz_name, "media") )
373     {
374         vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
375     }
376     else if( !strcmp(psz_name, "schedule") )
377     {
378         vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
379     }
380     else if( !strcmp(psz_name, "all") )
381     {
382         vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
383         vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
384     }
385     else
386     {
387         *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
388         return VLC_EGENERIC;
389     }
390
391     *pp_status = vlm_MessageNew( "del", NULL );
392     return VLC_SUCCESS;
393 }
394
395 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
396 {
397     vlm_media_sys_t *p_media;
398     vlm_schedule_sys_t *p_schedule;
399
400     if( !psz_name )
401     {
402         *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
403         return VLC_SUCCESS;
404     }
405
406     p_media = vlm_MediaSearch( p_vlm, psz_name );
407     p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
408
409     if( p_schedule != NULL )
410         *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
411     else if( p_media != NULL )
412         *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
413     else
414         *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
415
416     return VLC_SUCCESS;
417 }
418
419 static int ExecuteHelp( vlm_t *p_vlm, vlm_message_t **pp_status )
420 {
421     *pp_status = vlm_Help( p_vlm, NULL );
422     return VLC_SUCCESS;
423 }
424
425 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
426 {
427     vlm_media_sys_t *p_media;
428     const char *psz_control = NULL;
429     const char *psz_instance = NULL;
430     const char *psz_argument = NULL;
431     int i_index;
432     int i_result;
433
434     if( !ExecuteIsMedia( p_vlm, psz_name ) )
435     {
436         *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
437         return VLC_EGENERIC;
438     }
439
440     assert( i_arg > 0 );
441
442 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
443     i_index = 0;
444     if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
445     {
446         i_index = 1;
447         psz_instance = ppsz_arg[0];
448
449         if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
450             return ExecuteSyntaxError( "control", pp_status );
451     }
452 #undef IS
453     psz_control = ppsz_arg[i_index];
454
455     if( i_index+1 < i_arg )
456         psz_argument = ppsz_arg[i_index+1];
457
458     p_media = vlm_MediaSearch( p_vlm, psz_name );
459     assert( p_media );
460
461     if( !strcmp( psz_control, "play" ) )
462     {
463         int i_input_index = 0;
464         int i;
465
466         if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
467         {
468             i_input_index = i-1;
469         }
470         else if( psz_argument )
471         {
472             int j;
473             vlm_media_t *p_cfg = &p_media->cfg;
474             for ( j=0; j < p_cfg->i_input; j++)
475             {
476                 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
477                 {
478                     i_input_index = j;
479                     break;
480                 }
481             }
482         }
483
484         if( p_media->cfg.b_vod )
485             i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL );    // we should get here now
486         else
487             i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
488     }
489     else if( !strcmp( psz_control, "seek" ) )
490     {
491         if( psz_argument )
492         {
493             vlc_bool_t b_relative;
494             if( psz_argument[0] == '+' || psz_argument[0] == '-' )
495                 b_relative = VLC_TRUE;
496             else
497                 b_relative = VLC_FALSE;
498
499             if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
500             {
501                 /* Time (ms or s) */
502                 int64_t i_new_time;
503
504                 if( strstr( psz_argument, "ms" ) )
505                     i_new_time =  1000 * (int64_t)atoi( psz_argument );
506                 else
507                     i_new_time = 1000000 * (int64_t)atoi( psz_argument );
508
509                 if( b_relative )
510                 {
511                     int64_t i_time = 0;
512                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
513                     i_new_time += i_time;
514                 }
515                 if( i_new_time < 0 )
516                     i_new_time = 0;
517                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
518             }
519             else
520             {
521                 /* Percent */
522                 double d_new_position = i18n_atof( psz_argument ) / 100.0;
523
524                 if( b_relative )
525                 {
526                     double d_position = 0.0;
527
528                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
529                     d_new_position += d_position;
530                 }
531                 if( d_new_position < 0.0 )
532                     d_new_position = 0.0;
533                 else if( d_new_position > 1.0 )
534                     d_new_position = 1.0;
535                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
536             }
537         }
538         else
539         {
540             i_result = VLC_EGENERIC;
541         }
542     }
543     else if( !strcmp( psz_control, "rewind" ) )
544     {
545         if( psz_argument )
546         {
547             const double d_scale = i18n_atof( psz_argument );
548             double d_position;
549
550             vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
551             d_position -= (d_scale / 1000.0);
552             if( d_position < 0.0 )
553                 d_position = 0.0;
554             i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
555         }
556         else
557         {
558             i_result = VLC_EGENERIC;
559         }
560     }
561     else if( !strcmp( psz_control, "forward" ) )
562     {
563         if( psz_argument )
564         {
565             const double d_scale = i18n_atof( psz_argument );
566             double d_position;
567
568             vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
569             d_position += (d_scale / 1000.0);
570             if( d_position > 1.0 )
571                 d_position = 1.0;
572             i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
573
574         }
575         else
576         {
577             i_result = VLC_EGENERIC;
578         }
579     }
580     else if( !strcmp( psz_control, "stop" ) )
581     {
582         i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
583     }
584     else if( !strcmp( psz_control, "pause" ) )
585     {
586         i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
587     }
588     else
589     {
590         i_result = VLC_EGENERIC;
591     }
592
593     if( i_result )
594     {
595         *pp_status = vlm_MessageNew( "control", "unknown error" );
596         return VLC_SUCCESS;
597     }
598     *pp_status = vlm_MessageNew( "control", NULL );
599     return VLC_SUCCESS;
600 }
601
602 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
603 {
604     char *psz_export = Save( p_vlm );
605
606     *pp_status = vlm_MessageNew( "export", psz_export );
607     free( psz_export );
608     return VLC_SUCCESS;
609 }
610
611 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
612 {
613     FILE *f = utf8_fopen( psz_file, "wt" );
614     char *psz_save;
615
616     if( !f )
617         goto error;
618
619     psz_save = Save( p_vlm );
620     if( psz_save == NULL )
621     {
622         fclose( f );
623         goto error;
624     }
625     fwrite( psz_save, strlen( psz_save ), 1, f );
626     free( psz_save );
627     fclose( f );
628
629     *pp_status = vlm_MessageNew( "save", NULL );
630     return VLC_SUCCESS;
631
632 error:
633     *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
634     return VLC_EGENERIC;
635 }
636
637 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
638 {
639     stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
640     int64_t i_size;
641     char *psz_buffer;
642
643     if( !p_stream )
644     {
645         *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
646         return VLC_EGENERIC;
647     }
648
649     /* FIXME needed ? */
650     if( stream_Seek( p_stream, 0 ) != 0 )
651     {
652         stream_Delete( p_stream );
653
654         *pp_status = vlm_MessageNew( "load", "Read file error" );
655         return VLC_EGENERIC;
656     }
657
658     i_size = stream_Size( p_stream );
659
660     psz_buffer = malloc( i_size + 1 );
661     if( !psz_buffer )
662     {
663         stream_Delete( p_stream );
664
665         *pp_status = vlm_MessageNew( "load", "Read file error" );
666         return VLC_EGENERIC;
667     }
668
669     stream_Read( p_stream, psz_buffer, i_size );
670     psz_buffer[i_size] = '\0';
671
672     stream_Delete( p_stream );
673
674     if( Load( p_vlm, psz_buffer ) )
675     {
676         free( psz_buffer );
677
678         *pp_status = vlm_MessageNew( "load", "Error while loading file" );
679         return VLC_EGENERIC;
680     }
681
682     free( psz_buffer );
683
684     *pp_status = vlm_MessageNew( "load", NULL );
685     return VLC_SUCCESS;
686 }
687
688 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
689                                     const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
690 {
691     const char *psz_cmd = b_new ? "new" : "setup";
692     int i;
693
694     for( i = 0; i < i_property; i++ )
695     {
696         if( !strcmp( ppsz_property[i], "enabled" ) ||
697             !strcmp( ppsz_property[i], "disabled" ) )
698         {
699             vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
700         }
701         else if( !strcmp( ppsz_property[i], "append" ) )
702         {
703             char *psz_line;
704             int j;
705             /* Beware: everything behind append is considered as
706              * command line */
707
708             if( ++i >= i_property )
709                 break;
710
711             psz_line = strdup( ppsz_property[i] );
712             for( j = i+1; j < i_property; j++ )
713             {
714                 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
715                 strcat( psz_line, " " );
716                 strcat( psz_line, ppsz_property[j] );
717             }
718
719             vlm_ScheduleSetup( p_schedule, "append", psz_line );
720             break;
721         }
722         else
723         {
724             if( i + 1 >= i_property )
725             {
726                 if( b_new )
727                     vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
728                 return ExecuteSyntaxError( psz_cmd, pp_status );
729             }
730
731             vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
732             i++;
733         }
734     }
735     *pp_status = vlm_MessageNew( psz_cmd, NULL );
736     return VLC_SUCCESS;
737 }
738
739 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
740                                  const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
741 {
742     const char *psz_cmd = b_new ? "new" : "setup";
743     vlm_media_t *p_cfg = NULL;
744     int i_result;
745     int i;
746
747 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
748     if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
749         ERROR( "unknown media" );
750
751 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
752     for( i = 0; i < i_property; i++ )
753     {
754         const char *psz_option = ppsz_property[i];
755         const char *psz_value = i+1 < i_property ? ppsz_property[i+1] :  NULL;
756
757         if( !strcmp( psz_option, "enabled" ) )
758         {
759             p_cfg->b_enabled = VLC_TRUE;
760         }
761         else if( !strcmp( psz_option, "disabled" ) )
762         {
763             p_cfg->b_enabled = VLC_FALSE;
764         }
765         else if( !strcmp( psz_option, "input" ) )
766         {
767             MISSING( "input" );
768             TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
769             i++;
770         }
771         else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
772         {
773             while( p_cfg->i_input > 0 )
774                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
775             i++;
776         }
777         else if( !strcmp( psz_option, "inputdel" ) )
778         {
779             int j;
780
781             MISSING( "inputdel" );
782
783             for( j = 0; j < p_cfg->i_input; j++ )
784             {
785                 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
786                 {
787                     TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
788                     break;
789                 }
790             }
791             i++;
792         }
793         else if( !strcmp( psz_option, "inputdeln" ) )
794         {
795             int i_index;
796
797             MISSING( "inputdeln" );
798             
799             i_index = atoi( psz_value );
800             if( i_index > 0 && i_index <= p_cfg->i_input )
801                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
802             i++;
803         }
804         else if( !strcmp( psz_option, "output" ) )
805         {
806             MISSING( "output" );
807
808             if( p_cfg->psz_output != NULL )
809                 free( p_cfg->psz_output );
810             p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
811             i++;
812         }
813         else if( !strcmp( psz_option, "option" ) )
814         {
815             MISSING( "option" );
816
817             TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
818             i++;
819         }
820         else if( !strcmp( psz_option, "loop" ) )
821         {
822             if( p_cfg->b_vod )
823                 ERROR( "invalid loop option for vod" );
824             p_cfg->broadcast.b_loop = VLC_TRUE;
825         }
826         else if( !strcmp( psz_option, "unloop" ) )
827         {
828             if( p_cfg->b_vod )
829                 ERROR( "invalid unloop option for vod" );
830             p_cfg->broadcast.b_loop = VLC_FALSE;
831         }
832         else if( !strcmp( psz_option, "mux" ) )
833         {
834             MISSING( "mux" );
835             if( !p_cfg->b_vod )
836                 ERROR( "invalid mux option for broadcast" );
837
838             if( p_cfg->vod.psz_mux )
839                 free( p_cfg->vod.psz_mux );
840             p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
841             i++;
842         }
843         else
844         {
845             fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
846             ERROR( "Wrong command syntax" );
847         }
848     }
849 #undef MISSING
850 #undef ERROR
851
852     /* */
853     i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
854     vlm_media_Delete( p_cfg );
855
856     *pp_status = vlm_MessageNew( psz_cmd, NULL );
857     return i_result;
858
859 error:
860     if( p_cfg )
861     {
862         if( b_new )
863             vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
864         vlm_media_Delete( p_cfg );
865     }
866     return VLC_EGENERIC;
867 }
868 static int ExecuteNew( vlm_t *p_vlm, const char *psz_name, const char *psz_type, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
869 {
870     /* Check name */
871     if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
872     {
873         *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
874         return VLC_EGENERIC;
875     }
876     if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
877     {
878         *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
879         return VLC_EGENERIC;
880     }
881     /* */
882     if( !strcmp( psz_type, "schedule" ) )
883     {
884         vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
885         if( !p_schedule )
886         {
887             *pp_status = vlm_MessageNew( "new", "could not create schedule" );
888             return VLC_EGENERIC;
889         }
890         return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
891     }
892     else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
893     {
894         vlm_media_t cfg;
895         int64_t id;
896
897         vlm_media_Init( &cfg );
898         cfg.psz_name = strdup( psz_name );
899         cfg.b_vod = !strcmp( psz_type, "vod" );
900
901         if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
902         {
903             vlm_media_Clean( &cfg );
904             *pp_status = vlm_MessageNew( "new", "could not create media" );
905             return VLC_EGENERIC;
906         }
907         vlm_media_Clean( &cfg );
908         return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
909     }
910     else
911     {
912         *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
913         return VLC_EGENERIC;
914     }
915 }
916
917 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
918 {
919     if( ExecuteIsSchedule( p_vlm, psz_name ) )
920     {
921         vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
922         return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
923     }
924     else if( ExecuteIsMedia( p_vlm, psz_name ) )
925     {
926         int64_t id;
927         if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
928             goto error;
929         return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
930     }
931
932 error:
933     *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
934     return VLC_EGENERIC;
935 }
936
937 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
938                            vlm_message_t **pp_message )
939 {
940     size_t i_command = 0;
941     char buf[strlen (psz_command) + 1], *psz_buf = buf;
942     char *ppsz_command[3+sizeof (buf) / 2];
943     vlm_message_t *p_message = NULL;
944
945     /* First, parse the line and cut it */
946     while( *psz_command != '\0' )
947     {
948         const char *psz_temp;
949
950         if(isspace (*psz_command))
951         {
952             psz_command++;
953             continue;
954         }
955
956         /* support for comments */
957         if( i_command == 0 && *psz_command == '#')
958         {
959             p_message = vlm_MessageNew( "", NULL );
960             goto success;
961         }
962
963         psz_temp = FindCommandEnd( psz_command );
964
965         if( psz_temp == NULL )
966         {
967             p_message = vlm_MessageNew( "Incomplete command", psz_command );
968             goto error;
969         }
970
971         assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
972
973         ppsz_command[i_command] = psz_buf;
974         memcpy (psz_buf, psz_command, psz_temp - psz_command);
975         psz_buf[psz_temp - psz_command] = '\0';
976
977         Unescape (psz_buf, psz_buf);
978
979         i_command++;
980         psz_buf += psz_temp - psz_command + 1;
981         psz_command = psz_temp;
982
983         assert (buf + sizeof (buf) >= psz_buf);
984     }
985
986     /*
987      * And then Interpret it
988      */
989
990 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error;  if( (cmd) ) goto error; goto success; }
991     if( i_command == 0 )
992     {
993         p_message = vlm_MessageNew( "", NULL );
994         goto success;
995     }
996     else IF_EXECUTE( "del",     (i_command != 2),   ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
997     else IF_EXECUTE( "show",    (i_command > 2),    ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
998     else IF_EXECUTE( "help",    (i_command != 1),   ExecuteHelp(p_vlm, &p_message) )
999     else IF_EXECUTE( "control", (i_command < 3),    ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1000     else IF_EXECUTE( "save",    (i_command != 2),   ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1001     else IF_EXECUTE( "export",  (i_command != 1),   ExecuteExport(p_vlm, &p_message) )
1002     else IF_EXECUTE( "load",    (i_command != 2),   ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1003     else IF_EXECUTE( "new",     (i_command < 3),    ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1004     else IF_EXECUTE( "setup",   (i_command < 2),    ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1005     else
1006     {
1007         p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1008         goto error;
1009     }
1010 #undef IF_EXECUTE
1011
1012 success:
1013     *pp_message = p_message;
1014     return VLC_SUCCESS;
1015
1016 syntax_error:
1017     return ExecuteSyntaxError( ppsz_command[0], pp_message );
1018
1019 error:
1020     *pp_message = p_message;
1021     return VLC_EGENERIC;
1022 }
1023
1024 /*****************************************************************************
1025  * Media handling
1026  *****************************************************************************/
1027 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1028 {
1029     int i;
1030
1031     for( i = 0; i < vlm->i_media; i++ )
1032     {
1033         if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1034             return vlm->media[i];
1035     }
1036
1037     return NULL;
1038 }
1039
1040 /*****************************************************************************
1041  * Schedule handling
1042  *****************************************************************************/
1043 static int64_t vlm_Date(void)
1044 {
1045 #ifdef WIN32
1046     struct timeb tm;
1047     ftime( &tm );
1048     return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1049 #else
1050     return mdate();
1051 #endif
1052 }
1053
1054 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1055 {
1056     vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1057
1058     if( !p_sched )
1059     {
1060         return NULL;
1061     }
1062
1063     if( !psz_name )
1064     {
1065         return NULL;
1066     }
1067
1068     p_sched->psz_name = strdup( psz_name );
1069     p_sched->b_enabled = VLC_FALSE;
1070     p_sched->i_command = 0;
1071     p_sched->command = NULL;
1072     p_sched->i_date = 0;
1073     p_sched->i_period = 0;
1074     p_sched->i_repeat = -1;
1075
1076     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1077
1078     return p_sched;
1079 }
1080
1081 /* for now, simple delete. After, del with options (last arg) */
1082 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched,
1083                          const char *psz_name )
1084 {
1085     if( sched == NULL ) return;
1086
1087     TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1088
1089     if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1090     free( sched->psz_name );
1091     while( sched->i_command )
1092     {
1093         char *psz_cmd = sched->command[0];
1094         TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1095         free( psz_cmd );
1096     }
1097     free( sched );
1098 }
1099
1100 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1101 {
1102     int i;
1103
1104     for( i = 0; i < vlm->i_schedule; i++ )
1105     {
1106         if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1107         {
1108             return vlm->schedule[i];
1109         }
1110     }
1111
1112     return NULL;
1113 }
1114
1115 /* Ok, setup schedule command will be able to support only one (argument value) at a time  */
1116 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1117                        const char *psz_value )
1118 {
1119     if( !strcmp( psz_cmd, "enabled" ) )
1120     {
1121         schedule->b_enabled = VLC_TRUE;
1122     }
1123     else if( !strcmp( psz_cmd, "disabled" ) )
1124     {
1125         schedule->b_enabled = VLC_FALSE;
1126     }
1127 #if !defined( UNDER_CE )
1128     else if( !strcmp( psz_cmd, "date" ) )
1129     {
1130         struct tm time;
1131         const char *p;
1132         time_t date;
1133
1134         time.tm_sec = 0;         /* seconds */
1135         time.tm_min = 0;         /* minutes */
1136         time.tm_hour = 0;        /* hours */
1137         time.tm_mday = 0;        /* day of the month */
1138         time.tm_mon = 0;         /* month */
1139         time.tm_year = 0;        /* year */
1140         time.tm_wday = 0;        /* day of the week */
1141         time.tm_yday = 0;        /* day in the year */
1142         time.tm_isdst = -1;       /* daylight saving time */
1143
1144         /* date should be year/month/day-hour:minutes:seconds */
1145         p = strchr( psz_value, '-' );
1146
1147         if( !strcmp( psz_value, "now" ) )
1148         {
1149             schedule->i_date = 0;
1150         }
1151         else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1152                                         &time.tm_min, &time.tm_sec ) != 3 )
1153                                         /* it must be a hour:minutes:seconds */
1154         {
1155             return 1;
1156         }
1157         else
1158         {
1159             unsigned i,j,k;
1160
1161             switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1162             {
1163                 case 1:
1164                     time.tm_sec = i;
1165                     break;
1166                 case 2:
1167                     time.tm_min = i;
1168                     time.tm_sec = j;
1169                     break;
1170                 case 3:
1171                     time.tm_hour = i;
1172                     time.tm_min = j;
1173                     time.tm_sec = k;
1174                     break;
1175                 default:
1176                     return 1;
1177             }
1178
1179             switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1180             {
1181                 case 1:
1182                     time.tm_mday = i;
1183                     break;
1184                 case 2:
1185                     time.tm_mon = i - 1;
1186                     time.tm_mday = j;
1187                     break;
1188                 case 3:
1189                     time.tm_year = i - 1900;
1190                     time.tm_mon = j - 1;
1191                     time.tm_mday = k;
1192                     break;
1193                 default:
1194                     return 1;
1195             }
1196
1197             date = mktime( &time );
1198             schedule->i_date = ((mtime_t) date) * 1000000;
1199         }
1200     }
1201     else if( !strcmp( psz_cmd, "period" ) )
1202     {
1203         struct tm time;
1204         const char *p;
1205         const char *psz_time = NULL, *psz_date = NULL;
1206         time_t date;
1207         unsigned i,j,k;
1208
1209         /* First, if date or period are modified, repeat should be equal to -1 */
1210         schedule->i_repeat = -1;
1211
1212         time.tm_sec = 0;         /* seconds */
1213         time.tm_min = 0;         /* minutes */
1214         time.tm_hour = 0;        /* hours */
1215         time.tm_mday = 0;        /* day of the month */
1216         time.tm_mon = 0;         /* month */
1217         time.tm_year = 0;        /* year */
1218         time.tm_wday = 0;        /* day of the week */
1219         time.tm_yday = 0;        /* day in the year */
1220         time.tm_isdst = -1;       /* daylight saving time */
1221
1222         /* date should be year/month/day-hour:minutes:seconds */
1223         p = strchr( psz_value, '-' );
1224         if( p )
1225         {
1226             psz_date = psz_value;
1227             psz_time = p + 1;
1228         }
1229         else
1230         {
1231             psz_time = psz_value;
1232         }
1233
1234         switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1235         {
1236             case 1:
1237                 time.tm_sec = i;
1238                 break;
1239             case 2:
1240                 time.tm_min = i;
1241                 time.tm_sec = j;
1242                 break;
1243             case 3:
1244                 time.tm_hour = i;
1245                 time.tm_min = j;
1246                 time.tm_sec = k;
1247                 break;
1248             default:
1249                 return 1;
1250         }
1251         if( psz_date )
1252         {
1253             switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1254             {
1255                 case 1:
1256                     time.tm_mday = i;
1257                     break;
1258                 case 2:
1259                     time.tm_mon = i;
1260                     time.tm_mday = j;
1261                     break;
1262                 case 3:
1263                     time.tm_year = i;
1264                     time.tm_mon = j;
1265                     time.tm_mday = k;
1266                     break;
1267                 default:
1268                     return 1;
1269             }
1270         }
1271
1272         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1273         date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1274         schedule->i_period = ((mtime_t) date) * 1000000;
1275     }
1276 #endif /* UNDER_CE */
1277     else if( !strcmp( psz_cmd, "repeat" ) )
1278     {
1279         int i;
1280
1281         if( sscanf( psz_value, "%d", &i ) == 1 )
1282         {
1283             schedule->i_repeat = i;
1284         }
1285         else
1286         {
1287             return 1;
1288         }
1289     }
1290     else if( !strcmp( psz_cmd, "append" ) )
1291     {
1292         char *command = strdup( psz_value );
1293
1294         TAB_APPEND( schedule->i_command, schedule->command, command );
1295     }
1296     else
1297     {
1298         return 1;
1299     }
1300     return 0;
1301 }
1302
1303 /*****************************************************************************
1304  * Message handling functions
1305  *****************************************************************************/
1306 vlm_message_t *vlm_MessageNew( const char *psz_name,
1307                                const char *psz_format, ... )
1308 {
1309     vlm_message_t *p_message;
1310     va_list args;
1311
1312     if( !psz_name ) return NULL;
1313
1314     p_message = malloc( sizeof(vlm_message_t) );
1315     if( !p_message)
1316     {
1317         return NULL;
1318     }
1319
1320     p_message->psz_value = 0;
1321
1322     if( psz_format )
1323     {
1324         va_start( args, psz_format );
1325         if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1326         {
1327             va_end( args );
1328             free( p_message );
1329             return NULL;
1330         }
1331         va_end( args );
1332     }
1333
1334     p_message->psz_name = strdup( psz_name );
1335     p_message->i_child = 0;
1336     p_message->child = NULL;
1337
1338     return p_message;
1339 }
1340
1341 void vlm_MessageDelete( vlm_message_t *p_message )
1342 {
1343     if( p_message->psz_name ) free( p_message->psz_name );
1344     if( p_message->psz_value ) free( p_message->psz_value );
1345     while( p_message->i_child-- )
1346         vlm_MessageDelete( p_message->child[p_message->i_child] );
1347     if( p_message->child ) free( p_message->child );
1348     free( p_message );
1349 }
1350
1351 /* Add a child */
1352 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1353                                vlm_message_t *p_child )
1354 {
1355     if( p_message == NULL ) return NULL;
1356
1357     if( p_child )
1358     {
1359         TAB_APPEND( p_message->i_child, p_message->child, p_child );
1360     }
1361
1362     return p_child;
1363 }
1364
1365 /*****************************************************************************
1366  * Misc utility functions
1367  *****************************************************************************/
1368 static vlm_message_t *vlm_ShowMedia( vlm_t *p_vlm, vlm_media_sys_t *p_media )
1369 {
1370     vlm_media_t *p_cfg = &p_media->cfg;
1371     vlm_message_t *p_msg;
1372     vlm_message_t *p_msg_sub;
1373     int i;
1374
1375     p_msg = vlm_MessageNew( p_cfg->psz_name, 0 );
1376     vlm_MessageAdd( p_msg,
1377                     vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1378     vlm_MessageAdd( p_msg,
1379                     vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1380
1381     if( p_cfg->b_vod )
1382         vlm_MessageAdd( p_msg,
1383                         vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1384     else
1385         vlm_MessageAdd( p_msg,
1386                         vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1387
1388     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", NULL ) );
1389     for( i = 0; i < p_cfg->i_input; i++ )
1390     {
1391         char *psz_tmp;
1392         asprintf( &psz_tmp, "%d", i+1 );
1393         vlm_MessageAdd( p_msg_sub,
1394                         vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1395         free( psz_tmp );
1396     }
1397
1398     vlm_MessageAdd( p_msg,
1399                     vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1400
1401     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", 0 ) );
1402     for( i = 0; i < p_cfg->i_option; i++ )
1403         vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], 0 ) );
1404
1405     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", NULL ) );
1406     for( i = 0; i < p_media->i_instance; i++ )
1407     {
1408         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1409         vlc_value_t val;
1410         vlm_message_t *p_msg_instance;
1411         char *psz_tmp;
1412
1413         val.i_int = END_S;
1414         if( p_instance->p_input )
1415             var_Get( p_instance->p_input, "state", &val );
1416
1417         p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , NULL ) );
1418
1419         vlm_MessageAdd( p_msg_instance,
1420                         vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1421         vlm_MessageAdd( p_msg_instance,
1422                         vlm_MessageNew( "state",
1423                             val.i_int == PLAYING_S ? "playing" :
1424                             val.i_int == PAUSE_S ? "paused" :
1425                             "stopped" ) );
1426
1427         /* FIXME should not do that this way */
1428         if( p_instance->p_input )
1429         {
1430 #define APPEND_INPUT_INFO( a, format, type ) \
1431             asprintf( &psz_tmp, format, \
1432                       var_Get ## type( p_instance->p_input, a ) ); \
1433             vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1434             free( psz_tmp );
1435             APPEND_INPUT_INFO( "position", "%f", Float );
1436             APPEND_INPUT_INFO( "time", I64Fi, Time );
1437             APPEND_INPUT_INFO( "length", I64Fi, Time );
1438             APPEND_INPUT_INFO( "rate", "%d", Integer );
1439             APPEND_INPUT_INFO( "title", "%d", Integer );
1440             APPEND_INPUT_INFO( "chapter", "%d", Integer );
1441             APPEND_INPUT_INFO( "seekable", "%d", Bool );
1442         }
1443 #undef APPEND_INPUT_INFO
1444         asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1445         vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1446         free( psz_tmp );
1447     }
1448     return p_msg;
1449 }
1450
1451 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1452                                 vlm_schedule_sys_t *schedule,
1453                                 const char *psz_filter )
1454 {
1455     if( media != NULL )
1456     {
1457         vlm_message_t *p_msg = vlm_MessageNew( "show", NULL );
1458         if( p_msg )
1459             vlm_MessageAdd( p_msg, vlm_ShowMedia( vlm, media ) );
1460         return p_msg;
1461     }
1462
1463     else if( schedule != NULL )
1464     {
1465         int i;
1466         vlm_message_t *msg;
1467         vlm_message_t *msg_schedule;
1468         vlm_message_t *msg_child;
1469         char buffer[100];
1470
1471         msg = vlm_MessageNew( "show", NULL );
1472         msg_schedule =
1473             vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1474
1475         vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1476
1477         vlm_MessageAdd( msg_schedule,
1478                         vlm_MessageNew( "enabled", schedule->b_enabled ?
1479                                         "yes" : "no" ) );
1480
1481 #if !defined( UNDER_CE )
1482         if( schedule->i_date != 0 )
1483         {
1484             struct tm date;
1485             time_t i_time = (time_t)( schedule->i_date / 1000000 );
1486             char *psz_date;
1487
1488 #ifdef HAVE_LOCALTIME_R
1489             localtime_r( &i_time, &date);
1490 #else
1491             struct tm *p_date = localtime( &i_time );
1492             date = *p_date;
1493 #endif
1494
1495             asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1496                       date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1497                       date.tm_hour, date.tm_min, date.tm_sec );
1498
1499             vlm_MessageAdd( msg_schedule,
1500                             vlm_MessageNew( "date", psz_date ) );
1501             free( psz_date );
1502         }
1503         else
1504         {
1505             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1506         }
1507
1508         if( schedule->i_period != 0 )
1509         {
1510             time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1511             struct tm date;
1512
1513             date.tm_sec = (int)( i_time % 60 );
1514             i_time = i_time / 60;
1515             date.tm_min = (int)( i_time % 60 );
1516             i_time = i_time / 60;
1517             date.tm_hour = (int)( i_time % 24 );
1518             i_time = i_time / 24;
1519             date.tm_mday = (int)( i_time % 30 );
1520             i_time = i_time / 30;
1521             /* okay, okay, months are not always 30 days long */
1522             date.tm_mon = (int)( i_time % 12 );
1523             i_time = i_time / 12;
1524             date.tm_year = (int)i_time;
1525
1526             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1527                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1528
1529             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1530         }
1531         else
1532         {
1533             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1534         }
1535 #endif /* UNDER_CE */
1536
1537         sprintf( buffer, "%d", schedule->i_repeat );
1538         vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1539
1540         msg_child =
1541             vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1542
1543         for( i = 0; i < schedule->i_command; i++ )
1544         {
1545            vlm_MessageAdd( msg_child,
1546                            vlm_MessageNew( schedule->command[i], NULL ) );
1547         }
1548
1549         return msg;
1550
1551     }
1552
1553     else if( psz_filter && !strcmp( psz_filter, "media" ) )
1554     {
1555         vlm_message_t *p_msg;
1556         vlm_message_t *p_msg_child;
1557         int i_vod = 0, i_broadcast = 0;
1558         int i;
1559         char *psz_count;
1560
1561         for( i = 0; i < vlm->i_media; i++ )
1562         {
1563             if( vlm->media[i]->cfg.b_vod )
1564                 i_vod++;
1565             else
1566                 i_broadcast++;
1567         }
1568
1569         asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1570
1571         p_msg = vlm_MessageNew( "show", NULL );
1572         p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1573         free( psz_count );
1574
1575         for( i = 0; i < vlm->i_media; i++ )
1576             vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm, vlm->media[i] ) );
1577
1578         return p_msg;
1579     }
1580
1581     else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1582     {
1583         int i;
1584         vlm_message_t *msg;
1585         vlm_message_t *msg_child;
1586
1587         msg = vlm_MessageNew( "show", NULL );
1588         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1589
1590         for( i = 0; i < vlm->i_schedule; i++ )
1591         {
1592             vlm_schedule_sys_t *s = vlm->schedule[i];
1593             vlm_message_t *msg_schedule;
1594             mtime_t i_time, i_next_date;
1595
1596             msg_schedule = vlm_MessageAdd( msg_child,
1597                                            vlm_MessageNew( s->psz_name, 0 ) );
1598             vlm_MessageAdd( msg_schedule,
1599                             vlm_MessageNew( "enabled", s->b_enabled ?
1600                                             "yes" : "no" ) );
1601
1602             /* calculate next date */
1603             i_time = vlm_Date();
1604             i_next_date = s->i_date;
1605
1606             if( s->i_period != 0 )
1607             {
1608                 int j = 0;
1609                 while( s->i_date + j * s->i_period <= i_time &&
1610                        s->i_repeat > j )
1611                 {
1612                     j++;
1613                 }
1614
1615                 i_next_date = s->i_date + j * s->i_period;
1616             }
1617
1618             if( i_next_date > i_time )
1619             {
1620                 time_t i_date = (time_t) (i_next_date / 1000000) ;
1621
1622 #if !defined( UNDER_CE )
1623 #ifdef HAVE_CTIME_R
1624                 char psz_date[500];
1625                 ctime_r( &i_date, psz_date );
1626 #else
1627                 char *psz_date = ctime( &i_date );
1628 #endif
1629
1630                 vlm_MessageAdd( msg_schedule,
1631                                 vlm_MessageNew( "next launch", psz_date ) );
1632 #endif
1633             }
1634         }
1635
1636         return msg;
1637     }
1638
1639     else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1640     {
1641         vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1642         vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1643
1644         vlm_MessageAdd( show1, show2->child[0] );
1645
1646         /* We must destroy the parent node "show" of show2
1647          * and not the children */
1648         free( show2->psz_name );
1649         free( show2 );
1650
1651         return show1;
1652     }
1653
1654     else
1655     {
1656         return vlm_MessageNew( "show", NULL );
1657     }
1658 }
1659
1660 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1661 {
1662     vlm_message_t *message, *message_child;
1663
1664 #define MessageAdd( a ) \
1665         vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1666 #define MessageAddChild( a ) \
1667         vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1668
1669     if( psz_filter == NULL )
1670     {
1671         message = vlm_MessageNew( "help", NULL );
1672
1673         message_child = MessageAdd( "Commands Syntax:" );
1674         MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1675         MessageAddChild( "setup (name) (properties)" );
1676         MessageAddChild( "show [(name)|media|schedule]" );
1677         MessageAddChild( "del (name)|all|media|schedule" );
1678         MessageAddChild( "control (name) [instance_name] (command)" );
1679         MessageAddChild( "save (config_file)" );
1680         MessageAddChild( "export" );
1681         MessageAddChild( "load (config_file)" );
1682
1683         message_child = MessageAdd( "Media Proprieties Syntax:" );
1684         MessageAddChild( "input (input_name)" );
1685         MessageAddChild( "inputdel (input_name)|all" );
1686         MessageAddChild( "inputdeln input_number" );
1687         MessageAddChild( "output (output_name)" );
1688         MessageAddChild( "option (option_name)[=value]" );
1689         MessageAddChild( "enabled|disabled" );
1690         MessageAddChild( "loop|unloop (broadcast only)" );
1691         MessageAddChild( "mux (mux_name)" );
1692
1693         message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1694         MessageAddChild( "enabled|disabled" );
1695         MessageAddChild( "append (command_until_rest_of_the_line)" );
1696         MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1697                          "(seconds)|now" );
1698         MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1699                          "(days)-(hours):(minutes):(seconds)" );
1700         MessageAddChild( "repeat (number_of_repetitions)" );
1701
1702         message_child = MessageAdd( "Control Commands Syntax:" );
1703         MessageAddChild( "play [input_number]" );
1704         MessageAddChild( "pause" );
1705         MessageAddChild( "stop" );
1706         MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
1707
1708         return message;
1709     }
1710
1711     return vlm_MessageNew( "help", NULL );
1712 }
1713
1714 /*****************************************************************************
1715  * Config handling functions
1716  *****************************************************************************/
1717 static int Load( vlm_t *vlm, char *file )
1718 {
1719     char *pf = file;
1720     int  i_line = 1;
1721
1722     while( *pf != '\0' )
1723     {
1724         vlm_message_t *message = NULL;
1725         int i_end = 0;
1726
1727         while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1728         {
1729             i_end++;
1730         }
1731
1732         if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1733         {
1734             pf[i_end] = '\0';
1735             i_end++;
1736             if( pf[i_end] == '\n' ) i_end++;
1737         }
1738
1739         if( *pf && ExecuteCommand( vlm, pf, &message ) )
1740         {
1741             if( message )
1742             {
1743                 if( message->psz_value )
1744                     msg_Err( vlm, "Load error on line %d: %s: %s",
1745                              i_line, message->psz_name, message->psz_value );
1746                 vlm_MessageDelete( message );
1747             }
1748             return 1;
1749         }
1750         if( message ) vlm_MessageDelete( message );
1751
1752         pf += i_end;
1753         i_line++;
1754     }
1755
1756     return 0;
1757 }
1758
1759 static char *Save( vlm_t *vlm )
1760 {
1761     char *save = NULL;
1762     char psz_header[] = "\n"
1763                         "# VLC media player VLM command batch\n"
1764                         "# http://www.videolan.org/vlc/\n\n" ;
1765     char *p;
1766     int i,j;
1767     int i_length = strlen( psz_header );
1768
1769     for( i = 0; i < vlm->i_media; i++ )
1770     {
1771         vlm_media_sys_t *media = vlm->media[i];
1772         vlm_media_t *p_cfg = &media->cfg;
1773
1774         if( p_cfg->b_vod )
1775             i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1776         else
1777             i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1778
1779         if( p_cfg->b_enabled == VLC_TRUE )
1780             i_length += strlen( "enabled" );
1781         else
1782             i_length += strlen( "disabled" );
1783
1784         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1785             i_length += strlen( " loop\n" );
1786         else
1787             i_length += strlen( "\n" );
1788
1789         for( j = 0; j < p_cfg->i_input; j++ )
1790             i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1791
1792         if( p_cfg->psz_output != NULL )
1793             i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1794
1795         for( j = 0; j < p_cfg->i_option; j++ )
1796             i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1797
1798         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1799             i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1800     }
1801
1802     for( i = 0; i < vlm->i_schedule; i++ )
1803     {
1804         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1805
1806         i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
1807
1808         if( schedule->b_enabled == VLC_TRUE )
1809         {
1810             i_length += strlen( "date //-:: enabled\n" ) + 14;
1811         }
1812         else
1813         {
1814             i_length += strlen( "date //-:: disabled\n" ) + 14;
1815         }
1816
1817
1818         if( schedule->i_period != 0 )
1819         {
1820             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
1821                 strlen( "period //-::\n" ) + 14;
1822         }
1823
1824         if( schedule->i_repeat >= 0 )
1825         {
1826             char buffer[12];
1827
1828             sprintf( buffer, "%d", schedule->i_repeat );
1829             i_length += strlen( "setup  repeat \n" ) +
1830                 strlen( schedule->psz_name ) + strlen( buffer );
1831         }
1832         else
1833         {
1834             i_length++;
1835         }
1836
1837         for( j = 0; j < schedule->i_command; j++ )
1838         {
1839             i_length += strlen( "setup  append \n" ) +
1840                 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1841         }
1842
1843     }
1844
1845     /* Don't forget the '\0' */
1846     i_length++;
1847     /* now we have the length of save */
1848
1849     p = save = malloc( i_length );
1850     *save = '\0';
1851
1852     p += sprintf( p, "%s", psz_header );
1853
1854     /* finally we can write in it */
1855     for( i = 0; i < vlm->i_media; i++ )
1856     {
1857         vlm_media_sys_t *media = vlm->media[i];
1858         vlm_media_t *p_cfg = &media->cfg;
1859
1860         if( p_cfg->b_vod )
1861             p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1862         else
1863             p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1864
1865         if( p_cfg->b_enabled )
1866             p += sprintf( p, "enabled" );
1867         else
1868             p += sprintf( p, "disabled" );
1869
1870         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1871             p += sprintf( p, " loop\n" );
1872         else
1873             p += sprintf( p, "\n" );
1874
1875         for( j = 0; j < p_cfg->i_input; j++ )
1876             p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1877
1878         if( p_cfg->psz_output )
1879             p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1880
1881         for( j = 0; j < p_cfg->i_option; j++ )
1882             p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1883
1884         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1885             p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1886     }
1887
1888     /* and now, the schedule scripts */
1889 #if !defined( UNDER_CE )
1890     for( i = 0; i < vlm->i_schedule; i++ )
1891     {
1892         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1893         struct tm date;
1894         time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1895
1896 #ifdef HAVE_LOCALTIME_R
1897         localtime_r( &i_time, &date);
1898 #else
1899         struct tm *p_date = localtime( &i_time );
1900         date = *p_date;
1901 #endif
1902
1903         p += sprintf( p, "new %s schedule ", schedule->psz_name);
1904
1905         if( schedule->b_enabled == VLC_TRUE )
1906         {
1907             p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1908                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1909                           date.tm_hour, date.tm_min, date.tm_sec );
1910         }
1911         else
1912         {
1913             p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1914                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1915                           date.tm_hour, date.tm_min, date.tm_sec);
1916         }
1917
1918         if( schedule->i_period != 0 )
1919         {
1920             p += sprintf( p, "setup %s ", schedule->psz_name );
1921
1922             i_time = (time_t) ( schedule->i_period / 1000000 );
1923
1924             date.tm_sec = (int)( i_time % 60 );
1925             i_time = i_time / 60;
1926             date.tm_min = (int)( i_time % 60 );
1927             i_time = i_time / 60;
1928             date.tm_hour = (int)( i_time % 24 );
1929             i_time = i_time / 24;
1930             date.tm_mday = (int)( i_time % 30 );
1931             i_time = i_time / 30;
1932             /* okay, okay, months are not always 30 days long */
1933             date.tm_mon = (int)( i_time % 12 );
1934             i_time = i_time / 12;
1935             date.tm_year = (int)i_time;
1936
1937             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1938                           date.tm_year, date.tm_mon, date.tm_mday,
1939                           date.tm_hour, date.tm_min, date.tm_sec);
1940         }
1941
1942         if( schedule->i_repeat >= 0 )
1943         {
1944             p += sprintf( p, "setup %s repeat %d\n",
1945                           schedule->psz_name, schedule->i_repeat );
1946         }
1947         else
1948         {
1949             p += sprintf( p, "\n" );
1950         }
1951
1952         for( j = 0; j < schedule->i_command; j++ )
1953         {
1954             p += sprintf( p, "setup %s append %s\n",
1955                           schedule->psz_name, schedule->command[j] );
1956         }
1957
1958     }
1959 #endif /* UNDER_CE */
1960
1961     return save;
1962 }
1963
1964 /*****************************************************************************
1965  *
1966  *****************************************************************************/
1967 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1968                                 const char *psz_id, int i_query, va_list args )
1969 {
1970     vlm_t *vlm = (vlm_t *)p_private;
1971     int i, i_ret;
1972     const char *psz;
1973     int64_t id;
1974
1975     if( !p_private || !p_vod_media )
1976         return VLC_EGENERIC;
1977
1978     vlc_mutex_lock( &vlm->lock );
1979
1980     /* Find media id */
1981     for( i = 0, id = -1; i < vlm->i_media; i++ )
1982     {
1983         if( p_vod_media == vlm->media[i]->vod.p_media )
1984         {
1985             id = vlm->media[i]->cfg.id;
1986             break;
1987         }
1988     }
1989     if( id == -1 )
1990     {
1991         vlc_mutex_unlock( &vlm->lock );
1992         return VLC_EGENERIC;
1993     }
1994
1995     switch( i_query )
1996     {
1997     case VOD_MEDIA_PLAY:
1998         psz = (const char *)va_arg( args, const char * );
1999         i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
2000         break;
2001
2002     case VOD_MEDIA_PAUSE:
2003         i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
2004         break;
2005
2006     case VOD_MEDIA_STOP:
2007         i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
2008         break;
2009
2010     case VOD_MEDIA_SEEK:
2011     {
2012         double d_position = (double)va_arg( args, double );
2013         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2014         break;
2015     }
2016
2017     case VOD_MEDIA_REWIND:
2018     {
2019         double d_scale = (double)va_arg( args, double );
2020         double d_position;
2021
2022         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2023         d_position -= (d_scale / 1000.0);
2024         if( d_position < 0.0 )
2025             d_position = 0.0;
2026         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2027         break;
2028     }
2029
2030     case VOD_MEDIA_FORWARD:
2031     {
2032         double d_scale = (double)va_arg( args, double );
2033         double d_position;
2034
2035         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2036         d_position += (d_scale / 1000.0);
2037         if( d_position > 1.0 )
2038             d_position = 1.0;
2039         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2040         break;
2041     }
2042
2043     default:
2044         i_ret = VLC_EGENERIC;
2045         break;
2046     }
2047
2048     vlc_mutex_unlock( &vlm->lock );
2049
2050     return i_ret;
2051 }
2052
2053
2054 /*****************************************************************************
2055  * Manage:
2056  *****************************************************************************/
2057 static int Manage( vlc_object_t* p_object )
2058 {
2059     vlm_t *vlm = (vlm_t*)p_object;
2060     int i, j;
2061     mtime_t i_lastcheck;
2062     mtime_t i_time;
2063
2064     i_lastcheck = vlm_Date();
2065
2066     while( !vlm->b_die )
2067     {
2068         char **ppsz_scheduled_commands = NULL;
2069         int    i_scheduled_commands = 0;
2070
2071         vlc_mutex_lock( &vlm->lock );
2072
2073         /* destroy the inputs that wants to die, and launch the next input */
2074         for( i = 0; i < vlm->i_media; i++ )
2075         {
2076             vlm_media_sys_t *p_media = vlm->media[i];
2077
2078             for( j = 0; j < p_media->i_instance; )
2079             {
2080                 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2081
2082                 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2083                 {
2084                     int i_new_input_index;
2085
2086                     /* */
2087                     i_new_input_index = p_instance->i_index + 1;
2088                     if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2089                         i_new_input_index = 0;
2090
2091                     /* FIXME implement multiple input with VOD */
2092                     if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2093                         vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2094                     else
2095                         vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2096
2097                     j = 0;
2098                 }
2099                 else
2100                 {
2101                     j++;
2102                 }
2103             }
2104         }
2105
2106         /* scheduling */
2107         i_time = vlm_Date();
2108
2109         for( i = 0; i < vlm->i_schedule; i++ )
2110         {
2111             mtime_t i_real_date = vlm->schedule[i]->i_date;
2112
2113             if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2114             {
2115                 if( vlm->schedule[i]->i_date == 0 ) // now !
2116                 {
2117                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2118                     i_real_date = i_time;
2119                 }
2120                 else if( vlm->schedule[i]->i_period != 0 )
2121                 {
2122                     int j = 0;
2123                     while( vlm->schedule[i]->i_date + j *
2124                            vlm->schedule[i]->i_period <= i_lastcheck &&
2125                            ( vlm->schedule[i]->i_repeat > j ||
2126                              vlm->schedule[i]->i_repeat == -1 ) )
2127                     {
2128                         j++;
2129                     }
2130
2131                     i_real_date = vlm->schedule[i]->i_date + j *
2132                         vlm->schedule[i]->i_period;
2133                 }
2134
2135                 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2136                 {
2137                     for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2138                     {
2139                         TAB_APPEND( i_scheduled_commands,
2140                                     ppsz_scheduled_commands,
2141                                     strdup(vlm->schedule[i]->command[j] ) );
2142                     }
2143                 }
2144             }
2145         }
2146         while( i_scheduled_commands )
2147         {
2148             vlm_message_t *message = NULL;
2149             char *psz_command = ppsz_scheduled_commands[0];
2150             ExecuteCommand( vlm, psz_command,&message );
2151
2152             /* for now, drop the message */
2153             vlm_MessageDelete( message );
2154             TAB_REMOVE( i_scheduled_commands,
2155                         ppsz_scheduled_commands,
2156                         psz_command );
2157             free( psz_command );
2158         }
2159
2160         i_lastcheck = i_time;
2161
2162         vlc_mutex_unlock( &vlm->lock );
2163
2164         msleep( 100000 );
2165     }
2166
2167     return VLC_SUCCESS;
2168 }
2169
2170 /* New API
2171  */
2172 /*
2173 typedef struct
2174 {
2175     struct
2176     {
2177         int i_connection_count;
2178         int i_connection_active;
2179     } vod;
2180     struct
2181     {
2182         int        i_count;
2183         vlc_bool_t b_playing;
2184         int        i_playing_index;
2185     } broadcast;
2186
2187 } vlm_media_status_t;
2188 */
2189
2190 /* */
2191 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2192 {
2193     int i;
2194
2195     for( i = 0; i < p_vlm->i_media; i++ )
2196     {
2197         if( p_vlm->media[i]->cfg.id == id )
2198             return p_vlm->media[i];
2199     }
2200     return NULL;
2201 }
2202 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2203 {
2204     int i;
2205
2206     for( i = 0; i < p_vlm->i_media; i++ )
2207     {
2208         if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2209             return p_vlm->media[i];
2210     }
2211     return NULL;
2212 }
2213 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2214 {
2215     int i;
2216
2217     if( !p_cfg || !p_cfg->psz_name ||
2218         !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2219         return VLC_EGENERIC;
2220
2221     for( i = 0; i < p_vlm->i_media; i++ )
2222     {
2223         if( p_vlm->media[i]->cfg.id == p_cfg->id )
2224             continue;
2225         if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2226             return VLC_EGENERIC;
2227     }
2228     return VLC_SUCCESS;
2229 }
2230
2231
2232 /* Called after a media description is changed/added */
2233 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2234 {
2235     vlm_media_t *p_cfg = &p_media->cfg;
2236     /* Check if we need to create/delete a vod media */
2237     if( p_cfg->b_vod )
2238     {
2239         if( !p_cfg->b_enabled && p_media->vod.p_media )
2240         {
2241             p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2242             p_media->vod.p_media = NULL;
2243         }
2244         else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2245         {
2246             /* Pre-parse the input */
2247             input_thread_t *p_input;
2248             char *psz_output;
2249             char *psz_header;
2250             char *psz_dup;
2251             int i;
2252
2253             input_ItemClean( &p_media->vod.item );
2254             input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2255
2256             if( p_cfg->psz_output )
2257                 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2258             else
2259                 asprintf( &psz_output, "#description" );
2260
2261             p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2262
2263             TAB_INIT( p_media->vod.item.i_options, p_media->vod.item.ppsz_options );
2264
2265             asprintf( &psz_dup, "sout=%s", psz_output);
2266             TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2267             for( i = 0; i < p_cfg->i_option; i++ )
2268             {
2269                 psz_dup = strdup( p_cfg->ppsz_option[i] );
2270                 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2271             }
2272             psz_dup = strdup( "no-sout-keep" );
2273             TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2274
2275             asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2276
2277             if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2278             {
2279                 while( !p_input->b_eof && !p_input->b_error )
2280                     msleep( 100000 );
2281
2282                 input_StopThread( p_input );
2283                 input_DestroyThreadExtended( p_input, NULL );
2284             }
2285             free( psz_output );
2286             free( psz_header );
2287
2288             if( p_cfg->vod.psz_mux )
2289             {
2290                 input_item_t item;
2291                 es_format_t es, *p_es = &es;
2292                 char fourcc[5];
2293
2294                 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2295                 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2296                 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2297
2298                 item = p_media->vod.item;
2299                 item.i_es = 1;
2300                 item.es = &p_es;
2301                 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2302
2303                 p_media->vod.p_media =
2304                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2305             }
2306             else
2307             {
2308                 p_media->vod.p_media =
2309                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2310             }
2311         }
2312     }
2313     else
2314     {
2315         /* TODO start media if needed */
2316     }
2317
2318     /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2319
2320     return VLC_SUCCESS;
2321 }
2322 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2323 {
2324     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2325
2326     /* */
2327     if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2328         return VLC_EGENERIC;
2329     if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2330         return VLC_EGENERIC;
2331
2332     if( 0 )
2333     {
2334         /* TODO check what are the changes being done (stop instance if needed) */
2335     }
2336
2337     vlm_media_Clean( &p_media->cfg );
2338     vlm_media_Copy( &p_media->cfg, p_cfg );
2339
2340     return vlm_OnMediaUpdate( p_vlm, p_media );
2341 }
2342 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2343 {
2344     vlm_media_sys_t *p_media;
2345
2346     if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2347     {
2348         msg_Err( p_vlm, "invalid media description" );
2349         return VLC_EGENERIC;
2350     }
2351     /* Check if we need to load the VOD server */
2352     if( p_cfg->b_vod && !p_vlm->i_vod )
2353     {
2354         p_vlm->p_vod = vlc_object_create( p_vlm, VLC_OBJECT_VOD );
2355         vlc_object_attach( p_vlm->p_vod, p_vlm );
2356         p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2357         if( !p_vlm->p_vod->p_module )
2358         {
2359             msg_Err( p_vlm, "cannot find vod server" );
2360             vlc_object_detach( p_vlm->p_vod );
2361             vlc_object_destroy( p_vlm->p_vod );
2362             p_vlm->p_vod = 0;
2363             return VLC_EGENERIC;
2364         }
2365
2366         p_vlm->p_vod->p_data = p_vlm;
2367         p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2368     }
2369     if( p_cfg->b_vod )
2370         p_vlm->i_vod++;
2371
2372     p_media = malloc( sizeof( vlm_media_sys_t ) );
2373     memset( p_media, 0, sizeof(vlm_media_sys_t) );
2374
2375     vlm_media_Copy( &p_media->cfg, p_cfg );
2376     p_media->cfg.id = p_vlm->i_id++;
2377     /* FIXME do we do something here if enabled is true ? */
2378
2379     input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2380
2381     p_media->vod.p_media = NULL;
2382     TAB_INIT( p_media->i_instance, p_media->instance );
2383
2384     /* */
2385     TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2386
2387     if( p_id )
2388         *p_id = p_media->cfg.id;
2389
2390     return vlm_OnMediaUpdate( p_vlm, p_media );
2391 }
2392
2393 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2394 {
2395     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2396
2397     if( !p_media )
2398         return VLC_EGENERIC;
2399
2400     while( p_media->i_instance > 0 )
2401         vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2402
2403     if( p_media->cfg.b_vod )
2404     {
2405         p_media->cfg.b_enabled = VLC_FALSE;
2406         vlm_OnMediaUpdate( p_vlm, p_media );
2407         p_vlm->i_vod--;
2408     }
2409
2410     vlm_media_Clean( &p_media->cfg );
2411
2412     input_ItemClean( &p_media->vod.item );
2413
2414     TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2415
2416     free( p_media );
2417
2418     /* Check if we need to unload the VOD server */
2419     if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2420     {
2421         module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2422         vlc_object_detach( p_vlm->p_vod );
2423         vlc_object_destroy( p_vlm->p_vod );
2424         p_vlm->p_vod = NULL;
2425     }
2426     return VLC_SUCCESS;
2427 }
2428
2429 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2430 {
2431     vlm_media_t **pp_dsc;
2432     int                     i_dsc;
2433     int i;
2434
2435     TAB_INIT( i_dsc, pp_dsc );
2436     for( i = 0; i < p_vlm->i_media; i++ )
2437     {
2438         vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2439         TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2440     }
2441
2442     *ppp_dsc = pp_dsc;
2443     *pi_dsc = i_dsc;
2444
2445     return VLC_SUCCESS;
2446 }
2447 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2448 {
2449     while( p_vlm->i_media > 0 )
2450         vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2451
2452     return VLC_SUCCESS;
2453 }
2454 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2455 {
2456     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2457     if( !p_media )
2458         return VLC_EGENERIC;
2459
2460     *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2461     return VLC_SUCCESS;
2462 }
2463 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2464 {
2465     vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2466     if( !p_media )
2467         return VLC_EGENERIC;
2468
2469     *p_id = p_media->cfg.id;
2470     return VLC_SUCCESS;
2471 }
2472
2473 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2474 {
2475     int i;
2476
2477     for( i = 0; i < p_media->i_instance; i++ )
2478     {
2479         const char *psz = p_media->instance[i]->psz_name;
2480         if( ( psz == NULL && psz_id == NULL ) ||
2481             ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2482             return p_media->instance[i];
2483     }
2484     return NULL;
2485 }
2486 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2487 {
2488     vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2489     if( !p_instance )
2490         return NULL;
2491
2492     memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2493
2494     p_instance->psz_name = NULL;
2495     if( psz_name )
2496         p_instance->psz_name = strdup( psz_name );
2497
2498     input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2499
2500     p_instance->i_index = 0;
2501     p_instance->b_sout_keep = VLC_FALSE;
2502     p_instance->p_input = NULL;
2503     p_instance->p_sout = NULL;
2504
2505     return p_instance;
2506 }
2507 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2508 {
2509     if( p_instance->p_input )
2510     {
2511         input_StopThread( p_instance->p_input );
2512         input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2513     }
2514     if( p_instance->p_sout )
2515         sout_DeleteInstance( p_instance->p_sout );
2516
2517     input_ItemClean( &p_instance->item );
2518     if( p_instance->psz_name )
2519         free( p_instance->psz_name );
2520     free( p_instance );
2521 }
2522
2523
2524 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2525 {
2526     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2527     vlm_media_instance_sys_t *p_instance;
2528     char *psz_log;
2529
2530     if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2531         return VLC_EGENERIC;
2532
2533     /* TODO support multiple input for VOD with sout-keep ? */
2534
2535     if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2536         return VLC_EGENERIC;
2537
2538     if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2539         return VLC_EGENERIC;
2540
2541     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2542     if( !p_instance )
2543     {
2544         vlm_media_t *p_cfg = &p_media->cfg;
2545         char *psz_keep;
2546         int i;
2547
2548         p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2549         if( !p_instance )
2550             return VLC_ENOMEM;
2551
2552         TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
2553
2554         if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2555         {
2556             char *psz_buffer;
2557             asprintf( &psz_buffer, "sout=%s%s%s",
2558                       p_cfg->psz_output ? p_cfg->psz_output : "",
2559                       (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2560                       psz_vod_output ? psz_vod_output : "" );
2561             TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_buffer );
2562         }
2563
2564         for( i = 0; i < p_cfg->i_option; i++ )
2565         {
2566             if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2567                 p_instance->b_sout_keep = VLC_TRUE;
2568             else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2569                 p_instance->b_sout_keep = VLC_FALSE;
2570             else
2571                 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, strdup( p_cfg->ppsz_option[i] ) );
2572         }
2573         /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2574          * FIXME implement input list for VOD (need sout-keep)
2575          * */
2576         if( !p_cfg->b_vod && p_instance->b_sout_keep )
2577             psz_keep = strdup( "sout-keep" );
2578         else
2579             psz_keep = strdup( "no-sout-keep" );
2580         TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_keep );
2581
2582         TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2583     }
2584
2585     /* Stop old instance */
2586     if( p_instance->p_input )
2587     {
2588         if( p_instance->i_index == i_input_index &&
2589             !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2590         {
2591             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2592                 var_SetInteger( p_instance->p_input, "state",  PLAYING_S );
2593             return VLC_SUCCESS;
2594         }
2595
2596         input_StopThread( p_instance->p_input );
2597         input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2598         if( !p_instance->b_sout_keep && p_instance->p_sout )
2599         {
2600             sout_DeleteInstance( p_instance->p_sout );
2601             p_instance->p_sout = NULL;
2602         }
2603     }
2604
2605     /* Start new one */
2606     p_instance->i_index = i_input_index;
2607     input_item_SetURI( &p_instance->item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2608
2609     asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2610     p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2611     if( !p_instance->p_input )
2612     {
2613         TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2614         vlm_MediaInstanceDelete( p_instance );
2615     }
2616     free( psz_log );
2617
2618     return VLC_SUCCESS;
2619 }
2620
2621 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2622 {
2623     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2624     vlm_media_instance_sys_t *p_instance;
2625
2626     if( !p_media )
2627         return VLC_EGENERIC;
2628
2629     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2630     if( !p_instance )
2631         return VLC_EGENERIC;
2632
2633     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2634
2635     vlm_MediaInstanceDelete( p_instance );
2636
2637     return VLC_SUCCESS;
2638 }
2639 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2640 {
2641     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2642     vlm_media_instance_sys_t *p_instance;
2643     int i_state;
2644
2645     if( !p_media )
2646         return VLC_EGENERIC;
2647
2648     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2649     if( !p_instance || !p_instance->p_input )
2650         return VLC_EGENERIC;
2651
2652     /* Toggle pause state */
2653     i_state = var_GetInteger( p_instance->p_input, "state" );
2654     if( i_state == PAUSE_S )
2655         var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2656     else if( i_state == PLAYING_S )
2657         var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2658     return VLC_SUCCESS;
2659 }
2660 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2661 {
2662     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2663     vlm_media_instance_sys_t *p_instance;
2664
2665     if( !p_media )
2666         return VLC_EGENERIC;
2667
2668     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2669     if( !p_instance || !p_instance->p_input )
2670         return VLC_EGENERIC;
2671
2672     if( pi_time )
2673         *pi_time = var_GetTime( p_instance->p_input, "time" );
2674     if( pd_position )
2675         *pd_position = var_GetFloat( p_instance->p_input, "position" );
2676     return VLC_SUCCESS;
2677 }
2678 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2679 {
2680     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2681     vlm_media_instance_sys_t *p_instance;
2682
2683     if( !p_media )
2684         return VLC_EGENERIC;
2685
2686     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2687     if( !p_instance || !p_instance->p_input )
2688         return VLC_EGENERIC;
2689
2690     if( i_time >= 0 )
2691         return var_SetTime( p_instance->p_input, "time", i_time );
2692     else if( d_position >= 0 && d_position <= 100 )
2693         return var_SetFloat( p_instance->p_input, "position", d_position );
2694     return VLC_EGENERIC;
2695 }
2696
2697 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2698 {
2699     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2700     vlm_media_instance_t **pp_idsc;
2701     int                              i_idsc;
2702     int i;
2703
2704     if( !p_media )
2705         return VLC_EGENERIC;
2706
2707     TAB_INIT( i_idsc, pp_idsc );
2708     for( i = 0; i < p_media->i_instance; i++ )
2709     {
2710         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2711         vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2712
2713         if( p_instance->psz_name )
2714             p_idsc->psz_name = strdup( p_instance->psz_name );
2715         if( p_instance->p_input )
2716         {
2717             p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2718             p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2719             p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2720             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2721                 p_idsc->b_paused = VLC_TRUE;
2722             p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2723         }
2724
2725         TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2726     }
2727     *ppp_idsc = pp_idsc;
2728     *pi_instance = i_idsc;
2729     return VLC_SUCCESS;
2730 }
2731 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2732 {
2733     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2734
2735     if( !p_media )
2736         return VLC_EGENERIC;
2737
2738     while( p_media->i_instance > 0 )
2739         vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2740
2741     return VLC_SUCCESS;
2742 }
2743
2744 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2745 {
2746     while( p_vlm->i_schedule > 0 )
2747         vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
2748
2749     return VLC_SUCCESS;
2750 }
2751 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2752 {
2753     vlm_media_t *p_dsc;
2754     vlm_media_t **pp_dsc;
2755     vlm_media_t ***ppp_dsc;
2756     vlm_media_instance_t ***ppp_idsc;
2757     const char *psz_id;
2758     const char *psz_vod;
2759     int64_t *p_id;
2760     int64_t id;
2761     int i_int;
2762     int *pi_int;
2763
2764     int64_t *pi_i64;
2765     int64_t i_i64;
2766     double *pd_double;
2767     double d_double;
2768
2769     switch( i_query )
2770     {
2771     /* Media control */
2772     case VLM_GET_MEDIAS:
2773         ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2774         pi_int = (int *)va_arg( args, int * );
2775         return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2776
2777     case VLM_CLEAR_MEDIAS:
2778         return vlm_ControlMediaClear( p_vlm );
2779
2780     case VLM_CHANGE_MEDIA:
2781         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2782         return vlm_ControlMediaChange( p_vlm, p_dsc );
2783
2784     case VLM_ADD_MEDIA:
2785         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2786         p_id = (int64_t*)va_arg( args, int64_t * );
2787         return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2788
2789     case VLM_DEL_MEDIA:
2790         id = (int64_t)va_arg( args, int64_t );
2791         return vlm_ControlMediaDel( p_vlm, id );
2792
2793     case VLM_GET_MEDIA:
2794         id = (int64_t)va_arg( args, int64_t );
2795         pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2796         return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2797
2798     case VLM_GET_MEDIA_ID:
2799         psz_id = (const char*)va_arg( args, const char * );
2800         p_id = (int64_t*)va_arg( args, int64_t * );
2801         return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2802
2803
2804     /* Media instance control */
2805     case VLM_GET_MEDIA_INSTANCES:
2806         id = (int64_t)va_arg( args, int64_t );
2807         ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2808         pi_int = (int *)va_arg( args, int *);
2809         return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2810
2811     case VLM_CLEAR_MEDIA_INSTANCES:
2812         id = (int64_t)va_arg( args, int64_t );
2813         return vlm_ControlMediaInstanceClear( p_vlm, id );
2814
2815
2816     case VLM_START_MEDIA_BROADCAST_INSTANCE:
2817         id = (int64_t)va_arg( args, int64_t );
2818         psz_id = (const char*)va_arg( args, const char* );
2819         i_int = (int)va_arg( args, int );
2820         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2821
2822     case VLM_START_MEDIA_VOD_INSTANCE:
2823         id = (int64_t)va_arg( args, int64_t );
2824         psz_id = (const char*)va_arg( args, const char* );
2825         i_int = (int)va_arg( args, int );
2826         psz_vod = (const char*)va_arg( args, const char* );
2827         if( !psz_vod )
2828             return VLC_EGENERIC;
2829         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2830
2831     case VLM_STOP_MEDIA_INSTANCE:
2832         id = (int64_t)va_arg( args, int64_t );
2833         psz_id = (const char*)va_arg( args, const char* );
2834         return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2835
2836     case VLM_PAUSE_MEDIA_INSTANCE:
2837         id = (int64_t)va_arg( args, int64_t );
2838         psz_id = (const char*)va_arg( args, const char* );
2839         return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2840
2841     case VLM_GET_MEDIA_INSTANCE_TIME:
2842         id = (int64_t)va_arg( args, int64_t );
2843         psz_id = (const char*)va_arg( args, const char* );
2844         pi_i64 = (int64_t*)va_arg( args, int64_t * );
2845         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2846     case VLM_GET_MEDIA_INSTANCE_POSITION:
2847         id = (int64_t)va_arg( args, int64_t );
2848         psz_id = (const char*)va_arg( args, const char* );
2849         pd_double = (double*)va_arg( args, double* );
2850         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2851
2852     case VLM_SET_MEDIA_INSTANCE_TIME:
2853         id = (int64_t)va_arg( args, int64_t );
2854         psz_id = (const char*)va_arg( args, const char* );
2855         i_i64 = (int64_t)va_arg( args, int64_t);
2856         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2857     case VLM_SET_MEDIA_INSTANCE_POSITION:
2858         id = (int64_t)va_arg( args, int64_t );
2859         psz_id = (const char*)va_arg( args, const char* );
2860         d_double = (double)va_arg( args, double );
2861         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2862
2863     case VLM_CLEAR_SCHEDULES:
2864         return vlm_ControlScheduleClear( p_vlm );
2865
2866     default:
2867         msg_Err( p_vlm, "unknown VLM query" );
2868         return VLC_EGENERIC;
2869     }
2870 }
2871 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2872 {
2873     va_list args;
2874     int     i_result;
2875
2876     va_start( args, i_query );
2877     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2878     va_end( args );
2879
2880     return i_result;
2881 }
2882
2883 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2884 {
2885     va_list args;
2886     int     i_result;
2887
2888     va_start( args, i_query );
2889
2890     vlc_mutex_lock( &p_vlm->lock );
2891     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2892     vlc_mutex_unlock( &p_vlm->lock );
2893
2894     va_end( args );
2895
2896     return i_result;
2897 }
2898
2899 #else /* ENABLE_VLM */
2900
2901 /* We just define an empty wrapper */
2902 vlm_t *__vlm_New( vlc_object_t *a )
2903 {
2904     msg_Err( a, "VideoLAN manager support is disabled" );
2905     return NULL;
2906 }
2907 void vlm_Delete( vlm_t *a )
2908 {
2909     ;
2910 }
2911 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2912 {
2913     return -1;
2914 }
2915 vlm_message_t *vlm_MessageNew( const char *psz_name,
2916                                const char *psz_format, ... )
2917 {
2918     return NULL;
2919 }
2920 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2921                                vlm_message_t *p_child )
2922 {
2923     return NULL;
2924 }
2925 void vlm_MessageDelete( vlm_message_t *a )
2926 {
2927     ;
2928 }
2929
2930 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2931 {
2932     return VLC_EGENERIC;
2933 }
2934
2935 #endif /* ENABLE_VLM */