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