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