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