]> git.sesse.net Git - vlc/blob - src/misc/vlm.c
1009edc983648de6ea40bbd0e017e85977fd8341
[vlc] / src / misc / vlm.c
1 /*****************************************************************************
2  * vlm.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <ctype.h>                                              /* tolower() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef ENABLE_VLM
35
36 #include <vlc/intf.h>
37 #include <vlc/input.h>
38
39 #ifdef HAVE_TIME_H
40 #   include <time.h>                                              /* ctime() */
41 #   include <sys/timeb.h>                                         /* ftime() */
42 #endif
43
44 #include "vlc_vlm.h"
45 #include "vlc_vod.h"
46
47 #define FREE( p ) \
48         if( p ) { free( p ); (p) = NULL; }
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
54 static vlm_message_t *vlm_Help( vlm_t *, char * );
55
56 static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
57 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );
58
59 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
60 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
61
62 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char * );
63
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66 static int ExecuteCommand( vlm_t *, char *, vlm_message_t ** );
67 static int Manage( vlc_object_t * );
68
69 /*****************************************************************************
70  * vlm_New:
71  *****************************************************************************/
72 vlm_t *__vlm_New ( vlc_object_t *p_this )
73 {
74     vlc_value_t lockval;
75     vlm_t *p_vlm = NULL;
76     char *psz_vlmconf;
77
78     /* to be sure to avoid multiple creation */
79     var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
80     var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
81     vlc_mutex_lock( lockval.p_address );
82
83     if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
84     {
85         msg_Info( p_this, "creating vlm" );
86         if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
87         {
88             vlc_mutex_unlock( lockval.p_address );
89             return NULL;
90         }
91
92         vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
93         p_vlm->i_media      = 0;
94         p_vlm->media        = NULL;
95         p_vlm->i_vod        = 0;
96         p_vlm->i_schedule   = 0;
97         p_vlm->schedule     = NULL;
98
99         vlc_object_yield( p_vlm );
100         vlc_object_attach( p_vlm, p_this->p_vlc );
101     }
102     vlc_mutex_unlock( lockval.p_address );
103
104     if( vlc_thread_create( p_vlm, "vlm thread",
105                            Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
106     {
107         vlc_mutex_destroy( &p_vlm->lock );
108         vlc_object_destroy( p_vlm );
109         return NULL;
110     }
111
112     /* Try loading the vlm conf file given by --vlm-conf */
113     psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
114
115     if( psz_vlmconf && *psz_vlmconf )
116     {
117         vlm_message_t *p_message = NULL;
118         char *psz_buffer = NULL;
119
120         msg_Dbg( p_this, "loading vlm conf ..." );
121         asprintf(&psz_buffer, "load %s", psz_vlmconf );
122         if( psz_buffer )
123         {
124             msg_Dbg( p_this, psz_buffer);
125             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
126                 msg_Warn( p_this, "error while loading the vlm conf file" );
127             }
128             free(p_message);
129             free(psz_buffer);
130         }
131    }
132    free(psz_vlmconf);
133
134    return p_vlm;
135 }
136
137 /*****************************************************************************
138  * vlm_Delete:
139  *****************************************************************************/
140 void vlm_Delete( vlm_t *p_vlm )
141 {
142     vlc_value_t lockval;
143
144     var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
145     vlc_mutex_lock( lockval.p_address );
146
147     vlc_object_release( p_vlm );
148
149     if( p_vlm->i_refcount > 0 )
150     {
151         vlc_mutex_unlock( lockval.p_address );
152         return;
153     }
154
155     p_vlm->b_die = VLC_TRUE;
156     vlc_thread_join( p_vlm );
157
158     vlc_mutex_destroy( &p_vlm->lock );
159
160     while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
161     FREE( p_vlm->media );
162
163     while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
164                                                    p_vlm->schedule[0], NULL );
165     FREE( p_vlm->schedule );
166
167     vlc_object_detach( p_vlm );
168     vlc_object_destroy( p_vlm );
169     vlc_mutex_unlock( lockval.p_address );
170 }
171
172 /*****************************************************************************
173  * vlm_ExecuteCommand:
174  *****************************************************************************/
175 int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
176                         vlm_message_t **pp_message)
177 {
178     int i_result;
179
180     vlc_mutex_lock( &p_vlm->lock );
181     i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
182     vlc_mutex_unlock( &p_vlm->lock );
183
184     return i_result;
185 }
186
187 /*****************************************************************************
188  * vlm_Save:
189  *****************************************************************************/
190 int vlm_Save( vlm_t *p_vlm, char *psz_file )
191 {
192     FILE *file;
193     char *psz_save;
194
195     if( !p_vlm || !psz_file ) return 1;
196
197     file = fopen( psz_file, "wt" );
198     if( file == NULL ) return 1;
199
200     psz_save = Save( p_vlm );
201     if( psz_save == NULL )
202     {
203         fclose( file );
204         return 1;
205     }
206     fwrite( psz_save, strlen( psz_save ), 1, file );
207     fclose( file );
208     free( psz_save );
209
210     return 0;
211 }
212
213 /*****************************************************************************
214  * vlm_Load:
215  *****************************************************************************/
216 int vlm_Load( vlm_t *p_vlm, char *psz_file )
217 {
218     FILE *file;
219     int64_t i_size;
220     char *psz_buffer;
221
222     if( !p_vlm || !psz_file ) return 1;
223
224     file = fopen( psz_file, "r" );
225     if( file == NULL ) return 1;
226
227     if( fseek( file, 0, SEEK_END) != 0 ) return 2;
228
229     i_size = ftell( file );
230     fseek( file, 0, SEEK_SET);
231     psz_buffer = malloc( i_size + 1 );
232     if( !psz_buffer )
233     {
234         fclose( file );
235         return 2;
236     }
237     fread( psz_buffer, 1, i_size, file);
238     psz_buffer[ i_size ] = '\0';
239     if( Load( p_vlm, psz_buffer ) )
240     {
241         free( psz_buffer );
242         return 3;
243     }
244     free( psz_buffer );
245
246     fclose( file );
247
248     return 0;
249 }
250
251 /*****************************************************************************
252  * FindEndCommand
253  *****************************************************************************/
254 static char *FindEndCommand( char *psz )
255 {
256     char *psz_sent = psz;
257
258     switch( *psz_sent )
259     {
260     case '\"':
261         psz_sent++;
262
263         while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
264         {
265             if( *psz_sent == '\'' )
266             {
267                 psz_sent = FindEndCommand( psz_sent );
268                 if( psz_sent == NULL ) return NULL;
269             }
270             else psz_sent++;
271         }
272
273         if( *psz_sent == '\"' )
274         {
275             psz_sent++;
276             return psz_sent;
277         }
278
279         /* *psz_sent == '\0' -> number of " is incorrect */
280         else return NULL;
281
282         break;
283
284     case '\'':
285         psz_sent++;
286
287         while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
288         {
289             if( *psz_sent == '\"' )
290             {
291                 psz_sent = FindEndCommand( psz_sent );
292                 if( psz_sent == NULL ) return NULL;
293             }
294             else psz_sent++;
295         }
296
297         if( *psz_sent == '\'' )
298         {
299             psz_sent++;
300             return psz_sent;
301         }
302
303         /* *psz_sent == '\0' -> number of " is incorrect */
304         else return NULL;
305
306         break;
307
308     default: /* now we can look for spaces */
309         while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
310         {
311             if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
312             {
313                 psz_sent = FindEndCommand( psz_sent );
314             }
315             else psz_sent++;
316         }
317
318         return psz_sent;
319     }
320 }
321
322 /*****************************************************************************
323  * ExecuteCommand: The main state machine
324  *****************************************************************************
325  * Execute a command which ends with '\0' (string)
326  *****************************************************************************/
327 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
328                            vlm_message_t **pp_message )
329 {
330     int i_command = 0;
331     char **ppsz_command = NULL;
332     char *psz_cmd = psz_command;
333     vlm_message_t *p_message = NULL;
334     int i, j;
335
336     /* First, parse the line and cut it */
337     while( *psz_cmd != '\0' )
338     {
339
340         if( *psz_cmd == ' ' || *psz_cmd == '\t' )
341         {
342             psz_cmd++;
343         }
344         else
345         {
346             char *psz_temp;
347             int   i_temp;
348
349             /* support for comments */
350             if( i_command == 0 && *psz_cmd == '#')
351             {
352                 p_message = vlm_MessageNew( "", NULL );
353                 goto success;
354             }
355
356             psz_temp = FindEndCommand( psz_cmd );
357
358             if( psz_temp == NULL ) goto error;
359
360             i_temp = psz_temp - psz_cmd;
361
362             ppsz_command = realloc( ppsz_command, (i_command + 1) *
363                                     sizeof(char*) );
364             ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
365             strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
366             ppsz_command[ i_command ][ i_temp ] = '\0';
367
368             i_command++;
369
370             psz_cmd = psz_temp;
371         }
372     }
373
374     /*
375      * And then Interpret it
376      */
377
378     if( i_command == 0 )
379     {
380         p_message = vlm_MessageNew( "", NULL );
381         goto success;
382     }
383
384     if( !strcmp(ppsz_command[0], "new") )
385     {
386         int i_type;
387
388         /* Check the number of arguments */
389         if( i_command < 3 ) goto syntax_error;
390
391         /* Get type */
392         if( !strcmp(ppsz_command[2], "vod") )
393         {
394             i_type = VOD_TYPE;
395         }
396         else if( !strcmp(ppsz_command[2], "broadcast") )
397         {
398             i_type = BROADCAST_TYPE;
399         }
400         else if( !strcmp(ppsz_command[2], "schedule") )
401         {
402             i_type = SCHEDULE_TYPE;
403         }
404         else
405         {
406             p_message =
407                 vlm_MessageNew( "new", "%s: Choose between vod, "
408                                 "broadcast or schedule", ppsz_command[1] );
409             goto error;
410         }
411
412         /* Check for forbidden media names */
413         if( !strcmp(ppsz_command[1], "all") ||
414             !strcmp(ppsz_command[1], "media") ||
415             !strcmp(ppsz_command[1], "schedule") )
416         {
417             p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
418                                         "\"schedule\" are reserved names" );
419             goto error;
420         }
421
422         /* Check the name is not already in use */
423         if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
424             vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
425         {
426             p_message = vlm_MessageNew( "new", "%s: Name already in use",
427                                         ppsz_command[1] );
428             goto error;
429         }
430
431         /* Schedule */
432         if( i_type == SCHEDULE_TYPE )
433         {
434             vlm_schedule_t *p_schedule;
435             p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
436             if( !p_schedule )
437             {
438                 p_message = vlm_MessageNew( "new", "could not create schedule" );
439                 goto error;
440             }
441         }
442
443         /* Media */
444         else
445         {
446             vlm_media_t *p_media;
447             p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
448             if( !p_media )
449             {
450                 p_message = vlm_MessageNew( "new", "could not create media" );
451                 goto error;
452             }
453         }
454
455         if( i_command <= 3 )
456         {
457             p_message = vlm_MessageNew( "new", NULL );
458             goto success;
459         }
460
461         /* Properties will be dealt with later on */
462     }
463
464     else if( !strcmp(ppsz_command[0], "setup") )
465     {
466         if( i_command < 2 ) goto syntax_error;
467
468         /* Properties will be dealt with later on */
469     }
470
471     else if( !strcmp(ppsz_command[0], "del") )
472     {
473         vlm_media_t *p_media;
474         vlm_schedule_t *p_schedule;
475
476         if( i_command < 2 ) goto syntax_error;
477
478         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
479         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
480
481         if( p_schedule != NULL )
482         {
483             vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
484         }
485         else if( p_media != NULL )
486         {
487             vlm_MediaDelete( p_vlm, p_media, NULL );
488         }
489         else if( !strcmp(ppsz_command[1], "media") )
490         {
491             while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
492                                                      NULL );
493         }
494         else if( !strcmp(ppsz_command[1], "schedule") )
495         {
496             while( p_vlm->i_schedule )
497                 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
498         }
499         else if( !strcmp(ppsz_command[1], "all") )
500         {
501             while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
502                                                      NULL );
503
504             while( p_vlm->i_schedule )
505                 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
506         }
507         else
508         {
509             p_message = vlm_MessageNew( "del", "%s: media unknown",
510                                       ppsz_command[1] );
511             goto error;
512         }
513
514         p_message = vlm_MessageNew( "del", NULL );
515         goto success;
516     }
517
518     else if( !strcmp(ppsz_command[0], "show") )
519     {
520         vlm_media_t *p_media;
521         vlm_schedule_t *p_schedule;
522
523         if( i_command == 1 )
524         {
525             p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
526             goto success;
527         }
528         else if( i_command > 2 ) goto syntax_error;
529
530         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
531         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
532
533         if( p_schedule != NULL )
534         {
535             p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
536         }
537         else if( p_media != NULL )
538         {
539             p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
540         }
541         else
542         {
543             p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
544         }
545
546         goto success;
547     }
548
549     else if( !strcmp(ppsz_command[0], "help") )
550     {
551         if( i_command != 1 ) goto syntax_error;
552
553         p_message = vlm_Help( p_vlm, NULL );
554         goto success;
555     }
556
557     else if( !strcmp(ppsz_command[0], "control") )
558     {
559         vlm_media_t *p_media;
560
561         if( i_command < 3 ) goto syntax_error;
562
563         if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
564         {
565             p_message = vlm_MessageNew( "control", "%s: media unknown",
566                                       ppsz_command[1] );
567             goto error;
568         }
569         else
570         {
571             char *psz_command, *psz_arg = 0, *psz_instance = 0;
572             int i_index = 2;
573
574             if( strcmp( ppsz_command[2], "play" ) &&
575                 strcmp( ppsz_command[2], "stop" ) &&
576                 strcmp( ppsz_command[2], "pause" ) &&
577                 strcmp( ppsz_command[2], "seek" ) )
578             {
579                 i_index++;
580                 psz_instance = ppsz_command[2];
581
582                 if( i_command < 4 ) goto syntax_error;
583             }
584
585             psz_command = ppsz_command[i_index];
586
587             if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
588
589             vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
590                              psz_arg );
591             p_message = vlm_MessageNew( "control", NULL );
592             goto success;
593         }
594     }
595
596     else if( !strcmp(ppsz_command[0], "save") )
597     {
598         if( i_command != 2 ) goto syntax_error;
599
600         if( vlm_Save( p_vlm, ppsz_command[1] ) )
601         {
602             p_message = vlm_MessageNew( "save", "Unable to save to file" );
603             goto error;
604         }
605         else
606         {
607             p_message = vlm_MessageNew( "save", NULL );
608             goto success;
609         }
610     }
611
612     else if( !strcmp(ppsz_command[0], "load") )
613     {
614         if( i_command != 2 ) goto syntax_error;
615
616         switch( vlm_Load( p_vlm, ppsz_command[1] ) )
617         {
618             case 0:
619                 p_message = vlm_MessageNew( "load", NULL );
620                 goto success;
621             case 2:
622                 p_message = vlm_MessageNew( "load", "read file error" );
623                 goto error;
624             case 3:
625                 p_message =
626                     vlm_MessageNew( "load", "error while loading file" );
627                 goto error;
628             default:
629                 p_message =
630                     vlm_MessageNew( "load", "Unable to load from file" );
631                 goto error;
632         }
633     }
634
635     else
636     {
637         p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
638         goto error;
639     }
640
641     /* Common code between "new" and "setup" */
642     if( !strcmp(ppsz_command[0], "new") ||
643         !strcmp(ppsz_command[0], "setup") )
644     {
645         int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
646         vlm_media_t *p_media;
647         vlm_schedule_t *p_schedule;
648
649         if( i_command < i_command_start ) goto syntax_error;
650
651         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
652         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
653
654         if( !p_media && !p_schedule )
655         {
656             p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
657                                         ppsz_command[1] );
658             goto error;
659         }
660
661         if( p_schedule != NULL )
662         {
663             for( i = i_command_start ; i < i_command ; i++ )
664             {
665                 if( !strcmp( ppsz_command[i], "enabled" ) ||
666                     !strcmp( ppsz_command[i], "disabled" ) )
667                 {
668                     vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
669                 }
670
671                 /* Beware: everything behind append is considered as
672                  * command line */
673                 else if( !strcmp( ppsz_command[i], "append" ) )
674                 {
675                     if( ++i >= i_command ) break;
676
677                     for( j = i + 1; j < i_command; j++ )
678                     {
679                         ppsz_command[i] =
680                             realloc( ppsz_command[i], strlen(ppsz_command[i]) +
681                                      strlen(ppsz_command[j]) + 1 + 1 );
682                         strcat( ppsz_command[i], " " );
683                         strcat( ppsz_command[i], ppsz_command[j] );
684                     }
685
686                     vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
687                                        ppsz_command[i] );
688                     break;
689                 }
690                 else
691                 {
692                     if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
693                     {
694                         vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
695                         p_message =
696                             vlm_MessageNew( ppsz_command[0],
697                                             "Wrong properties syntax" );
698                         goto error;
699                     }
700                     else if( i + 1 >= i_command )
701                     {
702                         p_message =
703                             vlm_MessageNew( ppsz_command[0],
704                                             "Wrong properties syntax" );
705                         goto error;
706                     }
707
708                     vlm_ScheduleSetup( p_schedule, ppsz_command[i],
709                                        ppsz_command[i+1] );
710                     i++;
711                 }
712             }
713         }
714
715         else if( p_media != NULL )
716         {
717             for( i = i_command_start ; i < i_command ; i++ )
718             {
719                 if( !strcmp( ppsz_command[i], "enabled" ) ||
720                     !strcmp( ppsz_command[i], "disabled" ) )
721                 {
722                     vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
723                 }
724                 else if( i + 1 >= i_command &&
725                          !strcmp( ppsz_command[i], "mux") )
726                 {
727                     if( p_media->i_type != VOD_TYPE )
728                     {
729                         p_message = vlm_MessageNew( ppsz_command[0],
730                                   "mux only available for broadcast" );
731                     }
732                     else
733                     {
734                         vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
735                                         ppsz_command[i+1] );
736                         i++;
737                     }
738                 }
739                 else if( !strcmp( ppsz_command[i], "loop" ) ||
740                          !strcmp( ppsz_command[i], "unloop" ) )
741                 {
742                     if( p_media->i_type != BROADCAST_TYPE )
743                     {
744                         p_message = vlm_MessageNew( ppsz_command[0],
745                                   "loop only available for broadcast" );
746                     }
747                     else
748                     {
749                         vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
750                     }
751                 }
752                 else
753                 {
754                     if( i + 1 >= i_command &&
755                         !strcmp(ppsz_command[0], "new") )
756                     {
757                         vlm_MediaDelete( p_vlm, p_media, NULL );
758                         p_message =
759                             vlm_MessageNew( ppsz_command[0],
760                                             "Wrong properties syntax" );
761                         goto error;
762                     }
763                     else if( i + 1 >= i_command )
764                     {
765                         p_message =
766                             vlm_MessageNew( ppsz_command[0],
767                                             "Wrong properties syntax" );
768                         goto error;
769                     }
770
771                     vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
772                                     ppsz_command[i+1] );
773                     i++;
774                 }
775             }
776         }
777
778         p_message = vlm_MessageNew( ppsz_command[0], NULL );
779         goto success;
780     }
781
782 success:
783     for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
784     FREE( ppsz_command );
785     *pp_message = p_message;
786
787     return VLC_SUCCESS;
788
789 syntax_error:
790     p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
791
792 error:
793     for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
794     FREE( ppsz_command );
795     *pp_message = p_message;
796
797     return VLC_EGENERIC;
798 }
799
800 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
801 {
802     int i;
803
804     for( i = 0; i < vlm->i_media; i++ )
805     {
806         if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
807         {
808             return vlm->media[i];
809         }
810     }
811
812     return NULL;
813 }
814
815 /*****************************************************************************
816  * Media handling
817  *****************************************************************************/
818 static vlm_media_instance_t *
819 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
820 {
821     int i;
822
823     for( i = 0; i < media->i_instance; i++ )
824     {
825         if( ( !psz_name && !media->instance[i]->psz_name ) ||
826             ( psz_name && media->instance[i]->psz_name &&
827               !strcmp( psz_name, media->instance[i]->psz_name ) ) )
828         {
829             return media->instance[i];
830         }
831     }
832
833     return NULL;
834 }
835
836 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
837 {
838     vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
839
840     if( !media )
841     {
842         msg_Err( vlm, "out of memory" );
843         return NULL;
844     }
845
846     /* Check if we need to load the VOD server */
847     if( i_type == VOD_TYPE && !vlm->i_vod )
848     {
849         vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
850         vlc_object_attach( vlm->vod, vlm );
851         vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
852         if( !vlm->vod->p_module )
853         {
854             msg_Err( vlm, "cannot find vod server" );
855             vlc_object_detach( vlm->vod );
856             vlc_object_destroy( vlm->vod );
857             vlm->vod = 0;
858             free( media );
859             return NULL;
860         }
861
862         vlm->vod->p_data = vlm;
863         vlm->vod->pf_media_control = vlm_MediaVodControl;
864     }
865
866     if( i_type == VOD_TYPE ) vlm->i_vod++;
867
868     media->psz_name = strdup( psz_name );
869     media->b_enabled = VLC_FALSE;
870     media->b_loop = VLC_FALSE;
871     media->vod_media = NULL;
872     media->psz_vod_output = NULL;
873     media->psz_mux = NULL;
874     media->i_input = 0;
875     media->input = NULL;
876     media->psz_output = NULL;
877     media->i_option = 0;
878     media->option = NULL;
879     media->i_type = i_type;
880     media->i_instance = 0;
881     media->instance = NULL;
882
883     media->item.psz_uri = strdup( psz_name );
884     vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
885
886     TAB_APPEND( vlm->i_media, vlm->media, media );
887
888     return media;
889 }
890
891 /* for now, simple delete. After, del with options (last arg) */
892 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
893 {
894     if( media == NULL ) return;
895
896     while( media->i_instance )
897     {
898         vlm_media_instance_t *p_instance = media->instance[0];
899         vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
900     }
901
902     TAB_REMOVE( vlm->i_media, vlm->media, media );
903
904     if( media->i_type == VOD_TYPE )
905     {
906         vlm_MediaSetup( vlm, media, "disabled", 0 );
907         vlm->i_vod--;
908     }
909
910     /* Check if we need to unload the VOD server */
911     if( media->i_type == VOD_TYPE && !vlm->i_vod )
912     {
913         module_Unneed( vlm->vod, vlm->vod->p_module );
914         vlc_object_detach( vlm->vod );
915         vlc_object_destroy( vlm->vod );
916         vlm->vod = 0;
917     }
918
919     if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
920
921     free( media->psz_name );
922
923     while( media->i_input-- ) free( media->input[media->i_input] );
924     if( media->input ) free( media->input );
925
926     if( media->psz_output ) free( media->psz_output );
927     if( media->psz_mux ) free( media->psz_mux );
928
929     while( media->i_option-- ) free( media->option[media->i_option] );
930     if( media->option ) free( media->option );
931
932     vlc_input_item_Clean( &media->item );
933
934     free( media );
935 }
936
937 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
938                     char *psz_value )
939 {
940     if( !psz_cmd) return VLC_EGENERIC;
941
942     if( !strcmp( psz_cmd, "loop" ) )
943     {
944         media->b_loop = VLC_TRUE;
945     }
946     else if( !strcmp( psz_cmd, "unloop" ) )
947     {
948         media->b_loop = VLC_FALSE;
949     }
950     else if( !strcmp( psz_cmd, "enabled" ) )
951     {
952         media->b_enabled = VLC_TRUE;
953     }
954     else if( !strcmp( psz_cmd, "disabled" ) )
955     {
956         media->b_enabled = VLC_FALSE;
957     }
958     else if( !strcmp( psz_cmd, "mux" ) )
959     {
960         if( media->psz_mux ) free( media->psz_mux );
961         media->psz_mux = NULL;
962         if( psz_value ) media->psz_mux = strdup( psz_value );
963     }
964     else if( !strcmp( psz_cmd, "input" ) )
965     {
966         char *input;
967
968         if( psz_value != NULL && strlen(psz_value) > 1 &&
969             ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
970             ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
971               psz_value[ strlen(psz_value) - 1 ] == '\"' )  )
972         {
973             input = malloc( strlen(psz_value) - 1 );
974
975             memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
976             input[ strlen(psz_value) - 2 ] = '\0';
977         }
978         else
979         {
980             input = strdup( psz_value );
981         }
982
983         TAB_APPEND( media->i_input, media->input, input );
984     }
985     else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
986     {
987         while( media->i_input > 0 )
988         {
989             TAB_REMOVE( media->i_input, media->input, media->input[0] );
990         }
991     }
992     else if( !strcmp( psz_cmd, "inputdel" ) )
993     {
994         char *input;
995         int i;
996
997         if( psz_value != NULL && strlen(psz_value) > 1 &&
998             ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
999             ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1000               psz_value[ strlen(psz_value) - 1 ] == '\"' )  )
1001         {
1002             input = malloc( strlen(psz_value) - 1 );
1003
1004             memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1005             input[ strlen(psz_value) - 2 ] = '\0';
1006         }
1007         else
1008         {
1009             input = strdup( psz_value );
1010         }
1011
1012         for( i = 0; i < media->i_input; i++ )
1013         {
1014             if( !strcmp( input, media->input[i] ) )
1015             {
1016                 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1017                 break;
1018             }
1019         }
1020     }
1021     else if( !strcmp( psz_cmd, "inputdeln" ) )
1022     {
1023         int index = atoi( psz_value );
1024         if( index > 0 && index <= media->i_input )
1025         {
1026             TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1027         }
1028     }
1029     else if( !strcmp( psz_cmd, "output" ) )
1030     {
1031         if( media->psz_output != NULL )
1032         {
1033             free( media->psz_output );
1034         }
1035         media->psz_output = strdup( psz_value );
1036     }
1037     else if( !strcmp( psz_cmd, "option" ) )
1038     {
1039         char *psz_option;
1040         psz_option = strdup( psz_value );
1041
1042         TAB_APPEND( media->i_option, media->option, psz_option );
1043     }
1044     else
1045     {
1046         return VLC_EGENERIC;
1047     }
1048
1049     /* Check if we need to create/delete a vod media */
1050     if( media->i_type == VOD_TYPE )
1051     {
1052         if( !media->b_enabled && media->vod_media )
1053         {
1054             vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1055             media->vod_media = 0;
1056         }
1057         else if( media->b_enabled && !media->vod_media && media->i_input )
1058         {
1059             /* Pre-parse the input */
1060             input_thread_t *p_input;
1061             char *psz_output;
1062             int i;
1063
1064             vlc_input_item_Clean( &media->item );
1065             vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1066
1067             if( media->psz_output )
1068                 asprintf( &psz_output, "%s:description", media->psz_output );
1069             else
1070                 asprintf( &psz_output, "#description" );
1071
1072             media->item.psz_uri = strdup( media->input[0] );
1073             media->item.ppsz_options = malloc( sizeof( char* ) );
1074             asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1075             media->item.i_options = 1;
1076             for( i = 0; i < media->i_option; i++ )
1077             {
1078                 media->item.i_options++;
1079                 media->item.ppsz_options =
1080                     realloc( media->item.ppsz_options,
1081                              media->item.i_options * sizeof( char* ) );
1082                 media->item.ppsz_options[ media->item.i_options - 1 ] =
1083                     strdup( media->option[i] );
1084             }
1085
1086             if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1087             {
1088                 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1089
1090                 input_StopThread( p_input );
1091                 input_DestroyThread( p_input );
1092                 vlc_object_detach( p_input );
1093                 vlc_object_destroy( p_input );
1094             }
1095             free( psz_output );
1096
1097             if( media->psz_mux )
1098             {
1099                 input_item_t item;
1100                 es_format_t es, *p_es = &es;
1101                 char fourcc[5];
1102
1103                 sprintf( fourcc, "%4.4s", media->psz_mux );
1104                 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1105                 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1106
1107                 item = media->item;
1108                 item.i_es = 1;
1109                 item.es = &p_es;
1110                 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1111
1112                 media->vod_media =
1113                   vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1114                 return VLC_SUCCESS;
1115             }
1116
1117             media->vod_media =
1118                 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1119                                         &media->item );
1120         }
1121     }
1122
1123     return VLC_SUCCESS;
1124 }
1125
1126 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1127                       char *psz_command, char *psz_args )
1128 {
1129     vlm_media_instance_t *p_instance;
1130     int i;
1131
1132     p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1133
1134     if( !strcmp( psz_command, "play" ) )
1135     {
1136         if( !media->b_enabled || media->i_input == 0 ) return 0;
1137
1138         if( !p_instance )
1139         {
1140             p_instance = malloc( sizeof(vlm_media_instance_t) );
1141             if( !p_instance ) return VLC_EGENERIC;
1142             memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1143             vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1144             p_instance->p_input = NULL;
1145
1146             if( media->psz_output != NULL || media->psz_vod_output != NULL )
1147             {
1148                 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1149                 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1150                           media->psz_output ? media->psz_output : "",
1151                           (media->psz_output && media->psz_vod_output) ?
1152                           ":" : media->psz_vod_output ? "#" : "",
1153                           media->psz_vod_output ? media->psz_vod_output : "" );
1154                 p_instance->item.i_options = 1;
1155             }
1156
1157             for( i = 0; i < media->i_option; i++ )
1158             {
1159                 p_instance->item.i_options++;
1160                 p_instance->item.ppsz_options =
1161                     realloc( p_instance->item.ppsz_options,
1162                              p_instance->item.i_options * sizeof( char* ) );
1163                 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1164                     strdup( media->option[i] );
1165             }
1166
1167             p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1168             TAB_APPEND( media->i_instance, media->instance, p_instance );
1169         }
1170
1171         if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1172         {
1173             p_instance->i_index = i;
1174         }
1175
1176         if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1177         p_instance->item.psz_uri =
1178             strdup( media->input[p_instance->i_index] );
1179
1180         if( p_instance->p_input )
1181         {
1182             input_StopThread( p_instance->p_input );
1183             input_DestroyThread( p_instance->p_input );
1184             vlc_object_detach( p_instance->p_input );
1185             vlc_object_destroy( p_instance->p_input );
1186         }
1187
1188         p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1189         if( !p_instance->p_input )
1190         {
1191             TAB_REMOVE( media->i_instance, media->instance, p_instance );
1192             vlc_input_item_Clean( &p_instance->item );
1193             if( p_instance->psz_name ) free( p_instance->psz_name );
1194         }
1195
1196         return VLC_SUCCESS;
1197     }
1198
1199     if( !p_instance ) return VLC_EGENERIC;
1200
1201     if( !strcmp( psz_command, "seek" ) )
1202     {
1203         vlc_value_t val;
1204         float f_percentage;
1205
1206         if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1207         {
1208             val.f_float = f_percentage / 100.0 ;
1209             var_Set( p_instance->p_input, "position", val );
1210             return VLC_SUCCESS;
1211         }
1212     }
1213     else if( !strcmp( psz_command, "stop" ) )
1214     {
1215         TAB_REMOVE( media->i_instance, media->instance, p_instance );
1216
1217         if( p_instance->p_input )
1218         {
1219             input_StopThread( p_instance->p_input );
1220             input_DestroyThread( p_instance->p_input );
1221             vlc_object_detach( p_instance->p_input );
1222             vlc_object_destroy( p_instance->p_input );
1223         }
1224
1225         vlc_input_item_Clean( &p_instance->item );
1226         if( p_instance->psz_name ) free( p_instance->psz_name );
1227         free( p_instance );
1228
1229         return VLC_SUCCESS;
1230     }
1231     else if( !strcmp( psz_command, "pause" ) )
1232     {
1233         vlc_value_t val;
1234
1235         if( !p_instance->p_input ) return VLC_SUCCESS;
1236
1237         var_Get( p_instance->p_input, "state", &val );
1238
1239         if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1240         else val.i_int = PAUSE_S;
1241         var_Set( p_instance->p_input, "state", val );
1242
1243         return VLC_SUCCESS;
1244     }
1245
1246     return VLC_EGENERIC;
1247 }
1248
1249 /*****************************************************************************
1250  * Schedule handling
1251  *****************************************************************************/
1252 static int64_t vlm_Date()
1253 {
1254 #ifdef WIN32
1255     struct timeb tm;
1256     ftime( &tm );
1257     return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1258 #else
1259     return mdate();
1260 #endif
1261 }
1262
1263 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1264 {
1265     vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1266
1267     if( !p_sched )
1268     {
1269         return NULL;
1270     }
1271
1272     if( !psz_name )
1273     {
1274         return NULL;
1275     }
1276
1277     p_sched->psz_name = strdup( psz_name );
1278     p_sched->b_enabled = VLC_FALSE;
1279     p_sched->i_command = 0;
1280     p_sched->command = NULL;
1281     p_sched->i_date = 0;
1282     p_sched->i_period = 0;
1283     p_sched->i_repeat = -1;
1284
1285     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1286
1287     return p_sched;
1288 }
1289
1290 /* for now, simple delete. After, del with options (last arg) */
1291 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1292                          char *psz_name )
1293 {
1294     if( sched == NULL ) return;
1295
1296     TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1297
1298     if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1299     free( sched->psz_name );
1300     while( sched->i_command-- ) free( sched->command[sched->i_command] );
1301     free( sched );
1302 }
1303
1304 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1305 {
1306     int i;
1307
1308     for( i = 0; i < vlm->i_schedule; i++ )
1309     {
1310         if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1311         {
1312             return vlm->schedule[i];
1313         }
1314     }
1315
1316     return NULL;
1317 }
1318
1319 /* Ok, setup schedule command will be able to support only one (argument value) at a time  */
1320 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1321                        char *psz_value )
1322 {
1323     if( !strcmp( psz_cmd, "enabled" ) )
1324     {
1325         schedule->b_enabled = VLC_TRUE;
1326     }
1327     else if( !strcmp( psz_cmd, "disabled" ) )
1328     {
1329         schedule->b_enabled = VLC_FALSE;
1330     }
1331 #if !defined( UNDER_CE )
1332     else if( !strcmp( psz_cmd, "date" ) )
1333     {
1334         struct tm time;
1335         char *p;
1336         time_t date;
1337
1338         time.tm_sec = 0;         /* seconds */
1339         time.tm_min = 0;         /* minutes */
1340         time.tm_hour = 0;        /* hours */
1341         time.tm_mday = 0;        /* day of the month */
1342         time.tm_mon = 0;         /* month */
1343         time.tm_year = 0;        /* year */
1344         time.tm_wday = 0;        /* day of the week */
1345         time.tm_yday = 0;        /* day in the year */
1346         time.tm_isdst = 0;       /* daylight saving time */
1347
1348         /* date should be year/month/day-hour:minutes:seconds */
1349         p = strchr( psz_value, '-' );
1350
1351         if( !strcmp( psz_value, "now" ) )
1352         {
1353             schedule->i_date = 0;
1354         }
1355         else if( p == NULL && sscanf( psz_value, "%d:%d:%d", &time.tm_hour, &time.tm_min, &time.tm_sec ) != 3 ) /* it must be a hour:minutes:seconds */
1356         {
1357             return 1;
1358         }
1359         else
1360         {
1361             int i,j,k;
1362
1363             switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1364             {
1365                 case 1:
1366                     time.tm_sec = i;
1367                     break;
1368                 case 2:
1369                     time.tm_min = i;
1370                     time.tm_sec = j;
1371                     break;
1372                 case 3:
1373                     time.tm_hour = i;
1374                     time.tm_min = j;
1375                     time.tm_sec = k;
1376                     break;
1377                 default:
1378                     return 1;
1379             }
1380
1381             *p = '\0';
1382
1383             switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1384             {
1385                 case 1:
1386                     time.tm_mday = i;
1387                     break;
1388                 case 2:
1389                     time.tm_mon = i - 1;
1390                     time.tm_mday = j;
1391                     break;
1392                 case 3:
1393                     time.tm_year = i - 1900;
1394                     time.tm_mon = j - 1;
1395                     time.tm_mday = k;
1396                     break;
1397                 default:
1398                     return 1;
1399             }
1400
1401             date = mktime( &time );
1402             schedule->i_date = ((mtime_t) date) * 1000000;
1403         }
1404     }
1405     else if( !strcmp( psz_cmd, "period" ) )
1406     {
1407         struct tm time;
1408         char *p;
1409         char *psz_time = NULL, *psz_date = NULL;
1410         time_t date;
1411         int i,j,k;
1412
1413         /* First, if date or period are modified, repeat should be equal to -1 */
1414         schedule->i_repeat = -1;
1415
1416         time.tm_sec = 0;         /* seconds */
1417         time.tm_min = 0;         /* minutes */
1418         time.tm_hour = 0;        /* hours */
1419         time.tm_mday = 0;        /* day of the month */
1420         time.tm_mon = 0;         /* month */
1421         time.tm_year = 0;        /* year */
1422         time.tm_wday = 0;        /* day of the week */
1423         time.tm_yday = 0;        /* day in the year */
1424         time.tm_isdst = 0;       /* daylight saving time */
1425
1426         /* date should be year/month/day-hour:minutes:seconds */
1427         p = strchr( psz_value, '-' );
1428         if( p )
1429         {
1430             psz_date = psz_value;
1431             psz_time = p + 1;
1432
1433             *p = '\0';
1434         }
1435         else
1436         {
1437             psz_time = psz_value;
1438         }
1439
1440
1441         switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1442         {
1443             case 1:
1444                 time.tm_sec = i;
1445                 break;
1446             case 2:
1447                 time.tm_min = i;
1448                 time.tm_sec = j;
1449                 break;
1450             case 3:
1451                 time.tm_hour = i;
1452                 time.tm_min = j;
1453                 time.tm_sec = k;
1454                 break;
1455             default:
1456                 return 1;
1457         }
1458         if( psz_date )
1459         {
1460             switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1461             {
1462                 case 1:
1463                     time.tm_mday = i;
1464                     break;
1465                 case 2:
1466                     time.tm_mon = i;
1467                     time.tm_mday = j;
1468                     break;
1469                 case 3:
1470                     time.tm_year = i;
1471                     time.tm_mon = j;
1472                     time.tm_mday = k;
1473                     break;
1474                 default:
1475                     return 1;
1476             }
1477         }
1478
1479         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1480         date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1481         schedule->i_period = ((mtime_t) date) * 1000000;
1482     }
1483 #endif /* UNDER_CE */
1484     else if( !strcmp( psz_cmd, "repeat" ) )
1485     {
1486         int i;
1487
1488         if( sscanf( psz_value, "%d", &i ) == 1 )
1489         {
1490             schedule->i_repeat = i;
1491         }
1492         else
1493         {
1494             return 1;
1495         }
1496     }
1497     else if( !strcmp( psz_cmd, "append" ) )
1498     {
1499         char *command = strdup( psz_value );
1500
1501         TAB_APPEND( schedule->i_command, schedule->command, command );
1502     }
1503     else
1504     {
1505         return 1;
1506     }
1507     return 0;
1508 }
1509
1510 /*****************************************************************************
1511  * Message handling functions
1512  *****************************************************************************/
1513 static vlm_message_t *vlm_MessageNew( char *psz_name,
1514                                       const char *psz_format, ... )
1515 {
1516     vlm_message_t *p_message;
1517     va_list args;
1518
1519     if( !psz_name ) return 0;
1520
1521     p_message = malloc( sizeof(vlm_message_t) );
1522     if( !p_message)
1523     {
1524         return NULL;
1525     }
1526
1527     p_message->psz_value = 0;
1528
1529     if( psz_format )
1530     {
1531         va_start( args, psz_format );
1532         if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1533         {
1534             va_end( args );
1535             free( p_message );
1536             return 0;
1537         }
1538         va_end( args );
1539     }
1540
1541     p_message->psz_name = strdup( psz_name );
1542     p_message->i_child = 0;
1543     p_message->child = NULL;
1544
1545     return p_message;
1546 }
1547
1548 void vlm_MessageDelete( vlm_message_t *p_message )
1549 {
1550     if( p_message->psz_name ) free( p_message->psz_name );
1551     if( p_message->psz_value ) free( p_message->psz_value );
1552     while( p_message->i_child-- )
1553         vlm_MessageDelete( p_message->child[p_message->i_child] );
1554     if( p_message->child ) free( p_message->child );
1555     free( p_message );
1556 }
1557
1558 /* Add a child */
1559 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1560                                       vlm_message_t *p_child )
1561 {
1562     if( p_message == NULL ) return NULL;
1563
1564     if( p_child )
1565     {
1566         TAB_APPEND( p_message->i_child, p_message->child, p_child );
1567     }
1568
1569     return p_child;
1570 }
1571
1572 /*****************************************************************************
1573  * Misc utility functions
1574  *****************************************************************************/
1575 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1576                                 vlm_schedule_t *schedule, char *psz_filter )
1577 {
1578     if( media != NULL )
1579     {
1580         int i;
1581         vlm_message_t *msg;
1582         vlm_message_t *msg_media;
1583         vlm_message_t *msg_child;
1584
1585         msg = vlm_MessageNew( "show", NULL );
1586         msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1587
1588         vlm_MessageAdd( msg_media,
1589                         vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1590                                         "vod" : "broadcast" ) );
1591         vlm_MessageAdd( msg_media,
1592                         vlm_MessageNew( "enabled", media->b_enabled ?
1593                                         "yes" : "no" ) );
1594
1595         vlm_MessageAdd( msg_media,
1596                         vlm_MessageNew( "loop", media->b_loop ?
1597                                         "yes" : "no" ) );
1598
1599         if( media->i_type == VOD_TYPE && media->psz_mux )
1600             vlm_MessageAdd( msg_media,
1601                             vlm_MessageNew( "mux", media->psz_mux ) );
1602
1603         msg_child = vlm_MessageAdd( msg_media,
1604                                     vlm_MessageNew( "inputs", NULL ) );
1605
1606         for( i = 0; i < media->i_input; i++ )
1607         {
1608             vlm_MessageAdd( msg_child,
1609                             vlm_MessageNew( media->input[i], NULL ) );
1610         }
1611
1612         vlm_MessageAdd( msg_media,
1613                         vlm_MessageNew( "output", media->psz_output ?
1614                                         media->psz_output : "" ) );
1615
1616         msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1617
1618         for( i = 0; i < media->i_option; i++ )
1619         {
1620             vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1621         }
1622
1623         msg_child = vlm_MessageAdd( msg_media,
1624                                     vlm_MessageNew( "instances", NULL ) );
1625
1626         for( i = 0; i < media->i_instance; i++ )
1627         {
1628             vlm_media_instance_t *p_instance = media->instance[i];
1629             vlc_value_t val;
1630
1631             if( !p_instance->p_input ) val.i_int = END_S;
1632             else var_Get( p_instance->p_input, "state", &val );
1633
1634             vlm_MessageAdd( msg_child,
1635                 vlm_MessageNew( p_instance->psz_name ?
1636                                 p_instance->psz_name : "default",
1637                                 val.i_int == PLAYING_S ? "playing" :
1638                                 val.i_int == PAUSE_S ? "paused" :
1639                                 "stopped" ) );
1640         }
1641
1642         return msg;
1643
1644     }
1645
1646     else if( schedule != NULL )
1647     {
1648         int i;
1649         vlm_message_t *msg;
1650         vlm_message_t *msg_schedule;
1651         vlm_message_t *msg_child;
1652         char buffer[100];
1653
1654         msg = vlm_MessageNew( "show", NULL );
1655         msg_schedule =
1656             vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1657
1658         vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1659
1660         vlm_MessageAdd( msg_schedule,
1661                         vlm_MessageNew( "enabled", schedule->b_enabled ?
1662                                         "yes" : "no" ) );
1663
1664 #if !defined( UNDER_CE )
1665         if( schedule->i_date != 0 )
1666         {
1667             struct tm date;
1668             time_t i_time = (time_t)( schedule->i_date / 1000000 );
1669             char *psz_date;
1670
1671 #ifdef HAVE_LOCALTIME_R
1672             localtime_r( &i_time, &date);
1673 #else
1674             struct tm *p_date = localtime( &i_time );
1675             date = *p_date;
1676 #endif
1677
1678             asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1679                       date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1680                       date.tm_hour, date.tm_min, date.tm_sec );
1681
1682             vlm_MessageAdd( msg_schedule,
1683                             vlm_MessageNew( "date", psz_date ) );
1684             free( psz_date );
1685         }
1686         else
1687         {
1688             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1689         }
1690
1691         if( schedule->i_period != 0 )
1692         {
1693             time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1694             struct tm date;
1695
1696             date.tm_sec = (int)( i_time % 60 );
1697             i_time = i_time / 60;
1698             date.tm_min = (int)( i_time % 60 );
1699             i_time = i_time / 60;
1700             date.tm_hour = (int)( i_time % 24 );
1701             i_time = i_time / 24;
1702             date.tm_mday = (int)( i_time % 30 );
1703             i_time = i_time / 30;
1704             /* okay, okay, months are not always 30 days long */
1705             date.tm_mon = (int)( i_time % 12 );
1706             i_time = i_time / 12;
1707             date.tm_year = (int)i_time;
1708
1709             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1710                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1711
1712             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1713         }
1714         else
1715         {
1716             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1717         }
1718 #endif /* UNDER_CE */
1719
1720         sprintf( buffer, "%d", schedule->i_repeat );
1721         vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1722
1723         msg_child =
1724             vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1725
1726         for( i = 0; i < schedule->i_command; i++ )
1727         {
1728            vlm_MessageAdd( msg_child,
1729                            vlm_MessageNew( schedule->command[i], NULL ) );
1730         }
1731
1732         return msg;
1733
1734     }
1735
1736     else if( psz_filter && !strcmp( psz_filter, "media" ) )
1737     {
1738         int i, j;
1739         vlm_message_t *msg;
1740         vlm_message_t *msg_child;
1741
1742         msg = vlm_MessageNew( "show", NULL );
1743         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", NULL ) );
1744
1745         for( i = 0; i < vlm->i_media; i++ )
1746         {
1747             vlm_media_t *m = vlm->media[i];
1748             vlm_message_t *msg_media, *msg_instance;
1749
1750             msg_media = vlm_MessageAdd( msg_child,
1751                                         vlm_MessageNew( m->psz_name, 0 ) );
1752
1753             vlm_MessageAdd( msg_media,
1754                             vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1755                                             "vod" : "broadcast" ) );
1756
1757             vlm_MessageAdd( msg_media,
1758                             vlm_MessageNew( "enabled", m->b_enabled ?
1759                                             "yes" : "no" ) );
1760
1761             if( m->i_type == VOD_TYPE && m->psz_mux )
1762                 vlm_MessageAdd( msg_media,
1763                                 vlm_MessageNew( "mux", m->psz_mux ) );
1764
1765             msg_instance = vlm_MessageAdd( msg_media,
1766                                            vlm_MessageNew( "instances", 0 ) );
1767
1768             for( j = 0; j < m->i_instance; j++ )
1769             {
1770                 vlm_media_instance_t *p_instance = m->instance[j];
1771                 vlc_value_t val;
1772
1773                 if( !p_instance->p_input ) val.i_int = END_S;
1774                 else var_Get( p_instance->p_input, "state", &val );
1775
1776                 vlm_MessageAdd( msg_instance,
1777                     vlm_MessageNew( p_instance->psz_name ?
1778                                     p_instance->psz_name : "default",
1779                                     val.i_int == PLAYING_S ? "playing" :
1780                                     val.i_int == PAUSE_S ? "paused" :
1781                                     "stopped" ) );
1782             }
1783         }
1784
1785         return msg;
1786     }
1787
1788     else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1789     {
1790         int i;
1791         vlm_message_t *msg;
1792         vlm_message_t *msg_child;
1793
1794         msg = vlm_MessageNew( "show", NULL );
1795         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1796
1797         for( i = 0; i < vlm->i_schedule; i++ )
1798         {
1799             vlm_schedule_t *s = vlm->schedule[i];
1800             vlm_message_t *msg_schedule;
1801             mtime_t i_time, i_next_date;
1802
1803             msg_schedule = vlm_MessageAdd( msg_child,
1804                                            vlm_MessageNew( s->psz_name, 0 ) );
1805             vlm_MessageAdd( msg_schedule,
1806                             vlm_MessageNew( "enabled", s->b_enabled ?
1807                                             "yes" : "no" ) );
1808
1809             if( !s->b_enabled ) return msg;
1810
1811
1812             vlm_MessageAdd( msg_schedule,
1813                             vlm_MessageNew( "enabled", "yes" ) );
1814
1815             /* calculate next date */
1816             i_time = vlm_Date();
1817             i_next_date = s->i_date;
1818
1819             if( s->i_period != 0 )
1820             {
1821                 int j = 0;
1822                 while( s->i_date + j * s->i_period <= i_time &&
1823                        s->i_repeat > j )
1824                 {
1825                     j++;
1826                 }
1827
1828                 i_next_date = s->i_date + j * s->i_period;
1829             }
1830
1831             if( i_next_date > i_time )
1832             {
1833                 time_t i_date = (time_t) (i_next_date / 1000000) ;
1834
1835 #if !defined( UNDER_CE )
1836 #ifdef HAVE_CTIME_R
1837                 char psz_date[500];
1838                 ctime_r( &i_date, psz_date );
1839 #else
1840                 char *psz_date = ctime( &i_date );
1841 #endif
1842
1843                 vlm_MessageAdd( msg_schedule,
1844                                 vlm_MessageNew( "next launch", psz_date ) );
1845 #endif
1846             }
1847         }
1848
1849         return msg;
1850     }
1851
1852     else if( psz_filter == NULL && media == NULL && schedule == NULL )
1853     {
1854         vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1855         vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1856
1857         vlm_MessageAdd( show1, show2->child[0] );
1858
1859         /* We must destroy the parent node "show" of show2
1860          * and not the children */
1861         free( show2->psz_name );
1862         free( show2 );
1863
1864         return show1;
1865     }
1866
1867     else
1868     {
1869         return vlm_MessageNew( "show", NULL );
1870     }
1871 }
1872
1873 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1874 {
1875     vlm_message_t *message, *message_child;
1876
1877 #define MessageAdd( a ) \
1878         vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1879 #define MessageAddChild( a ) \
1880         vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1881
1882     if( psz_filter == NULL )
1883     {
1884         message = vlm_MessageNew( "help", NULL );
1885
1886         message_child = MessageAdd( "Commands Syntax:" );
1887         MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1888         MessageAddChild( "setup (name) (properties)" );
1889         MessageAddChild( "show [(name)|media|schedule]" );
1890         MessageAddChild( "del (name)|all|media|schedule" );
1891         MessageAddChild( "control (name) [instance_name] (command)" );
1892         MessageAddChild( "save (config_file)" );
1893         MessageAddChild( "load (config_file)" );
1894
1895         message_child = MessageAdd( "Media Proprieties Syntax:" );
1896         MessageAddChild( "input (input_name)" );
1897         MessageAddChild( "inputdel (input_name)|all" );
1898         MessageAddChild( "inputdeln input_number" );
1899         MessageAddChild( "output (output_name)" );
1900         MessageAddChild( "enabled|disabled" );
1901         MessageAddChild( "loop|unloop (broadcast only)" );
1902         MessageAddChild( "mux (mux_name)" );
1903
1904         message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1905         MessageAddChild( "enabled|disabled" );
1906         MessageAddChild( "append (command_until_rest_of_the_line)" );
1907         MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1908                          "(seconds)|now" );
1909         MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1910                          "(days)-(hours):(minutes):(seconds)" );
1911         MessageAddChild( "repeat (number_of_repetitions)" );
1912
1913         message_child = MessageAdd( "Control Commands Syntax:" );
1914         MessageAddChild( "play" );
1915         MessageAddChild( "pause" );
1916         MessageAddChild( "stop" );
1917         MessageAddChild( "seek (percentage)" );
1918
1919         return message;
1920     }
1921
1922     return vlm_MessageNew( "help", NULL );
1923 }
1924
1925 /*****************************************************************************
1926  * Config handling functions
1927  *****************************************************************************/
1928 static int Load( vlm_t *vlm, char *file )
1929 {
1930     char *pf = file;
1931
1932     while( *pf != '\0' )
1933     {
1934         vlm_message_t *message = NULL;
1935         int i_temp = 0;
1936         int i_next;
1937
1938         while( pf[i_temp] != '\n' && pf[i_temp] != '\0' && pf[i_temp] != '\r' )
1939         {
1940             i_temp++;
1941         }
1942
1943         if( pf[i_temp] == '\r' || pf[i_temp] == '\n' )
1944         {
1945             pf[i_temp] = '\0';
1946             i_next = i_temp + 1;
1947         }
1948         else
1949         {
1950             i_next = i_temp;
1951         }
1952
1953         if( ExecuteCommand( vlm, pf, &message ) )
1954         {
1955             free( message );
1956             return 1;
1957         }
1958         free( message );
1959
1960         pf += i_next;
1961     }
1962
1963     return 0;
1964 }
1965
1966 static char *Save( vlm_t *vlm )
1967 {
1968     char *save = NULL;
1969     char *p;
1970     int i,j;
1971     int i_length = 0;
1972
1973     for( i = 0; i < vlm->i_media; i++ )
1974     {
1975         vlm_media_t *media = vlm->media[i];
1976
1977         if( media->i_type == VOD_TYPE )
1978         {
1979             i_length += strlen( "new  vod " ) + strlen(media->psz_name);
1980         }
1981         else
1982         {
1983             i_length += strlen( "new  broadcast " ) + strlen(media->psz_name);
1984         }
1985
1986         if( media->b_enabled == VLC_TRUE )
1987         {
1988             i_length += strlen( "enabled" );
1989         }
1990         else
1991         {
1992             i_length += strlen( "disabled" );
1993         }
1994
1995         if( media->b_loop == VLC_TRUE )
1996         {
1997             i_length += strlen( " loop\n" );
1998         }
1999         else
2000         {
2001             i_length += strlen( "\n" );
2002         }
2003
2004         for( j = 0; j < media->i_input; j++ )
2005         {
2006             i_length += strlen( "setup  input \"\"\n" ) +
2007                 strlen( media->psz_name ) + strlen( media->input[j] );
2008         }
2009
2010         if( media->psz_output != NULL )
2011         {
2012             i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2013                 strlen( "setup  output \n" );
2014         }
2015
2016         for( j=0 ; j < media->i_option ; j++ )
2017         {
2018             i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2019                 strlen("setup  option \n");
2020         }
2021     }
2022
2023     for( i = 0; i < vlm->i_schedule; i++ )
2024     {
2025         vlm_schedule_t *schedule = vlm->schedule[i];
2026
2027         i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
2028
2029         if( schedule->b_enabled == VLC_TRUE )
2030         {
2031             i_length += strlen( "date //-:: enabled\n" ) + 14;
2032         }
2033         else
2034         {
2035             i_length += strlen( "date //-:: disabled\n" ) + 14;
2036         }
2037
2038
2039         if( schedule->i_period != 0 )
2040         {
2041             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
2042                 strlen( "period //-::\n" ) + 14;
2043         }
2044
2045         if( schedule->i_repeat >= 0 )
2046         {
2047             char buffer[12];
2048
2049             sprintf( buffer, "%d", schedule->i_repeat );
2050             i_length += strlen( "setup  repeat \n" ) +
2051                 strlen( schedule->psz_name ) + strlen( buffer );
2052         }
2053         else
2054         {
2055             i_length++;
2056         }
2057
2058         for( j = 0; j < schedule->i_command; j++ )
2059         {
2060             i_length += strlen( "setup  append \n" ) +
2061                 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2062         }
2063
2064     }
2065
2066     /* Don't forget the '\0' */
2067     i_length++;
2068     /* now we have the length of save */
2069
2070     p = save = malloc( i_length );
2071     *save = '\0';
2072
2073     /* finally we can write in it */
2074     for( i = 0; i < vlm->i_media; i++ )
2075     {
2076         vlm_media_t *media = vlm->media[i];
2077
2078         if( media->i_type == VOD_TYPE )
2079         {
2080             p += sprintf( p, "new %s vod ", media->psz_name);
2081         }
2082         else
2083         {
2084             p += sprintf( p, "new %s broadcast ", media->psz_name);
2085         }
2086
2087         if( media->b_enabled == VLC_TRUE )
2088         {
2089             p += sprintf( p, "enabled" );
2090         }
2091         else
2092         {
2093             p += sprintf( p, "disabled" );
2094         }
2095
2096         if( media->b_loop == VLC_TRUE )
2097         {
2098             p += sprintf( p, " loop\n" );
2099         }
2100         else
2101         {
2102             p += sprintf( p, "\n" );
2103         }
2104
2105         for( j = 0; j < media->i_input; j++ )
2106         {
2107             p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2108                           media->input[j] );
2109         }
2110
2111         if( media->psz_output != NULL )
2112         {
2113             p += sprintf( p, "setup %s output %s\n", media->psz_name,
2114                           media->psz_output );
2115         }
2116
2117         for( j = 0; j < media->i_option; j++ )
2118         {
2119             p += sprintf( p, "setup %s option %s\n", media->psz_name,
2120                           media->option[j] );
2121         }
2122     }
2123
2124     /* and now, the schedule scripts */
2125 #if !defined( UNDER_CE )
2126     for( i = 0; i < vlm->i_schedule; i++ )
2127     {
2128         vlm_schedule_t *schedule = vlm->schedule[i];
2129         struct tm date;
2130         time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2131
2132 #ifdef HAVE_LOCALTIME_R
2133         localtime_r( &i_time, &date);
2134 #else
2135         struct tm *p_date = localtime( &i_time );
2136         date = *p_date;
2137 #endif
2138
2139         p += sprintf( p, "new %s schedule ", schedule->psz_name);
2140
2141         if( schedule->b_enabled == VLC_TRUE )
2142         {
2143             p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2144                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2145                           date.tm_hour, date.tm_min, date.tm_sec );
2146         }
2147         else
2148         {
2149             p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2150                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2151                           date.tm_hour, date.tm_min, date.tm_sec);
2152         }
2153
2154         if( schedule->i_period != 0 )
2155         {
2156             p += sprintf( p, "setup %s ", schedule->psz_name );
2157
2158             i_time = (time_t) ( schedule->i_period / 1000000 );
2159
2160             date.tm_sec = (int)( i_time % 60 );
2161             i_time = i_time / 60;
2162             date.tm_min = (int)( i_time % 60 );
2163             i_time = i_time / 60;
2164             date.tm_hour = (int)( i_time % 24 );
2165             i_time = i_time / 24;
2166             date.tm_mday = (int)( i_time % 30 );
2167             i_time = i_time / 30;
2168             /* okay, okay, months are not always 30 days long */
2169             date.tm_mon = (int)( i_time % 12 );
2170             i_time = i_time / 12;
2171             date.tm_year = (int)i_time;
2172
2173             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2174                           date.tm_year, date.tm_mon, date.tm_mday,
2175                           date.tm_hour, date.tm_min, date.tm_sec);
2176         }
2177
2178         if( schedule->i_repeat >= 0 )
2179         {
2180             p += sprintf( p, "setup %s repeat %d\n",
2181                           schedule->psz_name, schedule->i_repeat );
2182         }
2183         else
2184         {
2185             p += sprintf( p, "\n" );
2186         }
2187
2188         for( j = 0; j < schedule->i_command; j++ )
2189         {
2190             p += sprintf( p, "setup %s append %s\n",
2191                           schedule->psz_name, schedule->command[j] );
2192         }
2193
2194     }
2195 #endif /* UNDER_CE */
2196
2197     return save;
2198 }
2199
2200 /*****************************************************************************
2201  * Manage:
2202  *****************************************************************************/
2203 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2204                          char *psz_id, int i_query, va_list args )
2205 {
2206     vlm_t *vlm = (vlm_t *)p_private;
2207     int i, i_ret = VLC_EGENERIC;
2208
2209     if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2210
2211     vlc_mutex_lock( &vlm->lock );
2212
2213     /* Find media */
2214     for( i = 0; i < vlm->i_media; i++ )
2215     {
2216         if( p_vod_media == vlm->media[i]->vod_media ) break;
2217     }
2218
2219     if( i == vlm->i_media )
2220     {
2221         vlc_mutex_unlock( &vlm->lock );
2222         return VLC_EGENERIC;
2223     }
2224
2225     switch( i_query )
2226     {
2227     case VOD_MEDIA_PLAY:
2228         vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2229         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2230         vlm->media[i]->psz_vod_output = 0;
2231         break;
2232
2233     case VOD_MEDIA_PAUSE:
2234         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2235         break;
2236
2237     case VOD_MEDIA_STOP:
2238         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2239         break;
2240
2241     case VOD_MEDIA_SEEK:
2242     {
2243         double f_pos = (double)va_arg( args, double );
2244         char psz_pos[50];
2245
2246         sprintf( psz_pos, "%f", f_pos );
2247         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2248         break;
2249     }
2250
2251     default:
2252         break;
2253     }
2254
2255     vlc_mutex_unlock( &vlm->lock );
2256
2257     return i_ret;
2258 }
2259
2260 /*****************************************************************************
2261  * Manage:
2262  *****************************************************************************/
2263 static int Manage( vlc_object_t* p_object )
2264 {
2265     vlm_t *vlm = (vlm_t*)p_object;
2266     int i, j;
2267     mtime_t i_lastcheck;
2268     mtime_t i_time;
2269
2270     i_lastcheck = vlm_Date();
2271
2272     msleep( 100000 );
2273
2274     while( !vlm->b_die )
2275     {
2276         vlc_mutex_lock( &vlm->lock );
2277
2278         /* destroy the inputs that wants to die, and launch the next input */
2279         for( i = 0; i < vlm->i_media; i++ )
2280         {
2281             vlm_media_t *p_media = vlm->media[i];
2282
2283             for( j = 0; j < p_media->i_instance; j++ )
2284             {
2285                 vlm_media_instance_t *p_instance = p_media->instance[j];
2286
2287                 if( !p_instance->p_input ||
2288                     ( !p_instance->p_input->b_eof &&
2289                       !p_instance->p_input->b_error ) ) continue;
2290
2291                 input_StopThread( p_instance->p_input );
2292                 input_DestroyThread( p_instance->p_input );
2293                 vlc_object_detach( p_instance->p_input );
2294                 vlc_object_destroy( p_instance->p_input );
2295
2296                 p_instance->i_index++;
2297                 if( p_instance->i_index == p_media->i_input &&
2298                     p_media->b_loop ) p_instance->i_index = 0;
2299
2300                 if( p_instance->i_index < p_media->i_input )
2301                 {
2302                     /* FIXME, find a way to select the right instance */
2303                     char buffer[12];
2304                     sprintf( buffer, "%d", p_instance->i_index );
2305                     vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2306                                       "play", buffer );
2307                 }
2308                 else
2309                 {
2310                     if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2311                                           "stop", 0 ) == VLC_SUCCESS ) i--;
2312                 }
2313             }
2314         }
2315
2316         /* scheduling */
2317         i_time = vlm_Date();
2318
2319         for( i = 0; i < vlm->i_schedule; i++ )
2320         {
2321             mtime_t i_real_date = vlm->schedule[i]->i_date;
2322
2323             if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2324             {
2325                 if( vlm->schedule[i]->i_date == 0 ) // now !
2326                 {
2327                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2328                     i_real_date = i_time;
2329                 }
2330                 else if( vlm->schedule[i]->i_period != 0 )
2331                 {
2332                     int j = 0;
2333                     while( vlm->schedule[i]->i_date + j *
2334                            vlm->schedule[i]->i_period <= i_lastcheck &&
2335                            ( vlm->schedule[i]->i_repeat > j ||
2336                              vlm->schedule[i]->i_repeat == -1 ) )
2337                     {
2338                         j++;
2339                     }
2340
2341                     i_real_date = vlm->schedule[i]->i_date + j *
2342                         vlm->schedule[i]->i_period;
2343                 }
2344
2345                 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2346                 {
2347                     for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2348                     {
2349                         vlm_message_t *message = NULL;
2350
2351                         ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2352                                         &message );
2353
2354                         /* for now, drop the message */
2355                         free( message );
2356                     }
2357                 }
2358             }
2359         }
2360
2361         i_lastcheck = i_time;
2362
2363         vlc_mutex_unlock( &vlm->lock );
2364
2365         msleep( 100000 );
2366     }
2367
2368     return VLC_SUCCESS;
2369 }
2370
2371 #else /* ENABLE_VLM */
2372
2373 /* We just define an empty wrapper */
2374 vlm_t *__vlm_New( vlc_object_t *a )
2375 {
2376     msg_Err( a, "VideoLAN manager support is disabled" );
2377     return 0;
2378 }
2379 void vlm_Delete( vlm_t *a ){}
2380 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2381 void vlm_MessageDelete( vlm_message_t *a ){}
2382 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2383 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2384 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2385 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2386     { return -1; }
2387 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2388 void  vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2389 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2390 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2391     { return -1; }
2392 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2393 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2394
2395 #endif /* ENABLE_VLM */