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