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