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