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