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