1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <ctype.h> /* tolower() */
43 #include <time.h> /* ctime() */
45 #include <vlc_input.h>
46 #include "input_internal.h"
47 #include <vlc_stream.h>
48 #include "vlm_internal.h"
49 #include <vlc_charset.h>
53 #include "../stream_output/stream_output.h"
54 #include "../libvlc.h"
56 /*****************************************************************************
58 *****************************************************************************/
61 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
63 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
65 static char *Save( vlm_t * );
66 static int Load( vlm_t *, char * );
68 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
69 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
70 const char *psz_value );
73 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
75 static const char quotes[] = "\"'";
77 * FindCommandEnd: look for the end of a possibly quoted string
78 * @return NULL on mal-formatted string,
79 * pointer past the last character otherwise.
81 static const char *FindCommandEnd( const char *psz_sent )
83 unsigned char c, quote = 0;
85 while( (c = *psz_sent) != '\0' )
89 if( strchr(quotes,c) ) // opening quote
91 else if( isspace(c) ) // non-escaped space
95 psz_sent++; // skip escaped character
96 if( *psz_sent == '\0' )
102 if( c == quote ) // non-escaped matching quote
104 else if( (quote == '"') && (c == '\\') )
106 psz_sent++; // skip escaped character
107 if (*psz_sent == '\0')
108 return NULL; // error, closing quote missing
114 // error (NULL) if we could not find a matching quote
115 return quote ? NULL : psz_sent;
120 * Unescape a nul-terminated string.
121 * Note that in and out can be identical.
123 * @param out output buffer (at least <strlen (in) + 1> characters long)
124 * @param in nul-terminated string to be unescaped
126 * @return 0 on success, -1 on error.
128 static int Unescape( char *out, const char *in )
130 unsigned char c, quote = 0;
133 while( (c = *in++) != '\0' )
135 // Don't escape the end of the string if we find a '#'
136 // that's the begining of a vlc command
137 // TODO: find a better solution
138 if( ( c == '#' && !quote ) || param )
147 if (strchr(quotes,c)) // opening quote
171 /* None of the special cases - copy the backslash */
177 if( c == quote ) // non-escaped matching quote
182 if( (quote == '"') && (c == '\\') )
191 case '\0': // should never happen
195 /* None of the special cases - copy the backslash */
207 /*****************************************************************************
208 * ExecuteCommand: The main state machine
209 *****************************************************************************
210 * Execute a command which ends with '\0' (string)
211 *****************************************************************************/
212 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
214 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
218 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
222 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
226 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
228 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
233 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
235 vlm_media_sys_t *p_media;
236 vlm_schedule_sys_t *p_schedule;
238 p_media = vlm_MediaSearch( p_vlm, psz_name );
239 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
241 if( p_schedule != NULL )
243 vlm_ScheduleDelete( p_vlm, p_schedule );
245 else if( p_media != NULL )
247 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
249 else if( !strcmp(psz_name, "media") )
251 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
253 else if( !strcmp(psz_name, "schedule") )
255 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
257 else if( !strcmp(psz_name, "all") )
259 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
260 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
264 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
268 *pp_status = vlm_MessageSimpleNew( "del" );
272 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
274 vlm_media_sys_t *p_media;
275 vlm_schedule_sys_t *p_schedule;
279 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
283 p_media = vlm_MediaSearch( p_vlm, psz_name );
284 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
286 if( p_schedule != NULL )
287 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
288 else if( p_media != NULL )
289 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
291 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
296 static int ExecuteHelp( vlm_message_t **pp_status )
298 vlm_message_t *message_child;
300 #define MessageAdd( a ) \
301 vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
302 #define MessageAddChild( a ) \
303 vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
305 *pp_status = vlm_MessageSimpleNew( "help" );
307 message_child = MessageAdd( "Commands Syntax:" );
308 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
309 MessageAddChild( "setup (name) (properties)" );
310 MessageAddChild( "show [(name)|media|schedule]" );
311 MessageAddChild( "del (name)|all|media|schedule" );
312 MessageAddChild( "control (name) [instance_name] (command)" );
313 MessageAddChild( "save (config_file)" );
314 MessageAddChild( "export" );
315 MessageAddChild( "load (config_file)" );
317 message_child = MessageAdd( "Media Proprieties Syntax:" );
318 MessageAddChild( "input (input_name)" );
319 MessageAddChild( "inputdel (input_name)|all" );
320 MessageAddChild( "inputdeln input_number" );
321 MessageAddChild( "output (output_name)" );
322 MessageAddChild( "option (option_name)[=value]" );
323 MessageAddChild( "enabled|disabled" );
324 MessageAddChild( "loop|unloop (broadcast only)" );
325 MessageAddChild( "mux (mux_name)" );
327 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
328 MessageAddChild( "enabled|disabled" );
329 MessageAddChild( "append (command_until_rest_of_the_line)" );
330 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
332 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
333 "(days)-(hours):(minutes):(seconds)" );
334 MessageAddChild( "repeat (number_of_repetitions)" );
336 message_child = MessageAdd( "Control Commands Syntax:" );
337 MessageAddChild( "play [input_number]" );
338 MessageAddChild( "pause" );
339 MessageAddChild( "stop" );
340 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
345 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
347 vlm_media_sys_t *p_media;
348 const char *psz_control = NULL;
349 const char *psz_instance = NULL;
350 const char *psz_argument = NULL;
354 if( !ExecuteIsMedia( p_vlm, psz_name ) )
356 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
362 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
364 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
367 psz_instance = ppsz_arg[0];
369 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
370 return ExecuteSyntaxError( "control", pp_status );
373 psz_control = ppsz_arg[i_index];
375 if( i_index+1 < i_arg )
376 psz_argument = ppsz_arg[i_index+1];
378 p_media = vlm_MediaSearch( p_vlm, psz_name );
381 if( !strcmp( psz_control, "play" ) )
383 int i_input_index = 0;
386 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
390 else if( psz_argument )
393 vlm_media_t *p_cfg = &p_media->cfg;
394 for ( j=0; j < p_cfg->i_input; j++)
396 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
404 if( p_media->cfg.b_vod )
405 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
407 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
409 else if( !strcmp( psz_control, "seek" ) )
414 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
419 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
424 if( strstr( psz_argument, "ms" ) )
425 i_new_time = 1000 * (int64_t)atoi( psz_argument );
427 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
432 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
433 i_new_time += i_time;
437 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
442 double d_new_position = us_atof( psz_argument ) / 100.0;
446 double d_position = 0.0;
448 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
449 d_new_position += d_position;
451 if( d_new_position < 0.0 )
452 d_new_position = 0.0;
453 else if( d_new_position > 1.0 )
454 d_new_position = 1.0;
455 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
460 i_result = VLC_EGENERIC;
463 else if( !strcmp( psz_control, "stop" ) )
465 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
467 else if( !strcmp( psz_control, "pause" ) )
469 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
473 i_result = VLC_EGENERIC;
478 *pp_status = vlm_MessageNew( "control", "unknown error" );
481 *pp_status = vlm_MessageSimpleNew( "control" );
485 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
487 char *psz_export = Save( p_vlm );
489 *pp_status = vlm_MessageNew( "export", "%s", psz_export );
494 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
496 FILE *f = vlc_fopen( psz_file, "wt" );
497 char *psz_save = NULL;
502 psz_save = Save( p_vlm );
503 if( psz_save == NULL )
505 if( fputs( psz_save, f ) == EOF )
515 *pp_status = vlm_MessageSimpleNew( "save" );
522 *pp_status = vlm_MessageNew( "save", "Unable to save to file");
526 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_path, vlm_message_t **pp_status )
528 char *psz_url = make_URI( psz_path, NULL );
529 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
536 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
541 if( stream_Seek( p_stream, 0 ) != 0 )
543 stream_Delete( p_stream );
545 *pp_status = vlm_MessageNew( "load", "Read file error" );
549 i_size = stream_Size( p_stream );
550 if( i_size > SIZE_MAX - 1 )
551 i_size = SIZE_MAX - 1;
553 psz_buffer = malloc( i_size + 1 );
556 stream_Delete( p_stream );
558 *pp_status = vlm_MessageNew( "load", "Read file error" );
562 stream_Read( p_stream, psz_buffer, i_size );
563 psz_buffer[i_size] = '\0';
565 stream_Delete( p_stream );
567 if( Load( p_vlm, psz_buffer ) )
571 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
577 *pp_status = vlm_MessageSimpleNew( "load" );
581 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
582 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
584 const char *psz_cmd = b_new ? "new" : "setup";
587 for( i = 0; i < i_property; i++ )
589 if( !strcmp( ppsz_property[i], "enabled" ) ||
590 !strcmp( ppsz_property[i], "disabled" ) )
592 if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
595 else if( !strcmp( ppsz_property[i], "append" ) )
599 /* Beware: everything behind append is considered as
602 if( ++i >= i_property )
605 psz_line = strdup( ppsz_property[i] );
606 for( j = i+1; j < i_property; j++ )
608 psz_line = xrealloc( psz_line,
609 strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
610 strcat( psz_line, " " );
611 strcat( psz_line, ppsz_property[j] );
614 if( vlm_ScheduleSetup( p_schedule, "append", psz_line ) )
620 if( i + 1 >= i_property )
623 vlm_ScheduleDelete( p_vlm, p_schedule );
624 return ExecuteSyntaxError( psz_cmd, pp_status );
627 if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
632 *pp_status = vlm_MessageSimpleNew( psz_cmd );
634 vlc_mutex_lock( &p_vlm->lock_manage );
635 p_vlm->input_state_changed = true;
636 vlc_cond_signal( &p_vlm->wait_manage );
637 vlc_mutex_unlock( &p_vlm->lock_manage );
642 *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
647 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
648 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
650 const char *psz_cmd = b_new ? "new" : "setup";
651 vlm_media_t *p_cfg = NULL;
657 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
658 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
659 ERROR( "unknown media" );
661 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
662 for( i = 0; i < i_property; i++ )
664 const char *psz_option = ppsz_property[i];
665 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
667 if( !strcmp( psz_option, "enabled" ) )
669 p_cfg->b_enabled = true;
671 else if( !strcmp( psz_option, "disabled" ) )
673 p_cfg->b_enabled = false;
675 else if( !strcmp( psz_option, "input" ) )
678 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
681 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
683 while( p_cfg->i_input > 0 )
684 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
687 else if( !strcmp( psz_option, "inputdel" ) )
691 MISSING( "inputdel" );
693 for( j = 0; j < p_cfg->i_input; j++ )
695 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
697 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
703 else if( !strcmp( psz_option, "inputdeln" ) )
707 MISSING( "inputdeln" );
709 i_index = atoi( psz_value );
710 if( i_index > 0 && i_index <= p_cfg->i_input )
711 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
714 else if( !strcmp( psz_option, "output" ) )
718 free( p_cfg->psz_output );
719 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
722 else if( !strcmp( psz_option, "option" ) )
726 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
729 else if( !strcmp( psz_option, "loop" ) )
732 ERROR( "invalid loop option for vod" );
733 p_cfg->broadcast.b_loop = true;
735 else if( !strcmp( psz_option, "unloop" ) )
738 ERROR( "invalid unloop option for vod" );
739 p_cfg->broadcast.b_loop = false;
741 else if( !strcmp( psz_option, "mux" ) )
745 ERROR( "invalid mux option for broadcast" );
747 free( p_cfg->vod.psz_mux );
748 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
753 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
754 ERROR( "Wrong command syntax" );
761 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
762 vlm_media_Delete( p_cfg );
764 *pp_status = vlm_MessageSimpleNew( psz_cmd );
771 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
772 vlm_media_Delete( p_cfg );
777 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 )
780 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
782 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
785 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
787 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
791 if( !strcmp( psz_type, "schedule" ) )
793 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
796 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
799 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
801 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
806 vlm_media_Init( &cfg );
807 cfg.psz_name = strdup( psz_name );
808 cfg.b_vod = !strcmp( psz_type, "vod" );
810 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
812 vlm_media_Clean( &cfg );
813 *pp_status = vlm_MessageNew( "new", "could not create media" );
816 vlm_media_Clean( &cfg );
817 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
821 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
826 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
828 if( ExecuteIsSchedule( p_vlm, psz_name ) )
830 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
831 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
833 else if( ExecuteIsMedia( p_vlm, psz_name ) )
836 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
838 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
842 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
846 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
847 vlm_message_t **pp_message )
849 size_t i_command = 0;
850 char buf[strlen (psz_command) + 1], *psz_buf = buf;
851 char *ppsz_command[3+sizeof (buf) / 2];
852 vlm_message_t *p_message = NULL;
854 /* First, parse the line and cut it */
855 while( *psz_command != '\0' )
857 const char *psz_temp;
859 if(isspace ((unsigned char)*psz_command))
865 /* support for comments */
866 if( i_command == 0 && *psz_command == '#')
868 p_message = vlm_MessageSimpleNew( "" );
872 psz_temp = FindCommandEnd( psz_command );
874 if( psz_temp == NULL )
876 p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
880 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
882 ppsz_command[i_command] = psz_buf;
883 memcpy (psz_buf, psz_command, psz_temp - psz_command);
884 psz_buf[psz_temp - psz_command] = '\0';
886 Unescape (psz_buf, psz_buf);
889 psz_buf += psz_temp - psz_command + 1;
890 psz_command = psz_temp;
892 assert (buf + sizeof (buf) >= psz_buf);
896 * And then Interpret it
899 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
902 p_message = vlm_MessageSimpleNew( "" );
905 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
906 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
907 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
908 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
909 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
910 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
911 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
912 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
913 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
916 p_message = vlm_MessageNew( ppsz_command[0], "Unknown VLM command" );
922 *pp_message = p_message;
926 return ExecuteSyntaxError( ppsz_command[0], pp_message );
929 *pp_message = p_message;
933 /*****************************************************************************
935 *****************************************************************************/
936 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
940 for( i = 0; i < vlm->i_media; i++ )
942 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
943 return vlm->media[i];
949 /*****************************************************************************
951 *****************************************************************************/
952 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
957 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
961 p_sched->psz_name = strdup( psz_name );
962 p_sched->b_enabled = false;
963 p_sched->i_command = 0;
964 p_sched->command = NULL;
966 p_sched->i_period = 0;
967 p_sched->i_repeat = -1;
969 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
974 /* for now, simple delete. After, del with options (last arg) */
975 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
977 if( sched == NULL ) return;
979 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
981 if( vlm->i_schedule == 0 ) free( vlm->schedule );
982 free( sched->psz_name );
983 while( sched->i_command )
985 char *psz_cmd = sched->command[0];
986 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
992 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
996 for( i = 0; i < vlm->i_schedule; i++ )
998 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1000 return vlm->schedule[i];
1007 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1008 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1009 const char *psz_value )
1011 if( !strcmp( psz_cmd, "enabled" ) )
1013 schedule->b_enabled = true;
1015 else if( !strcmp( psz_cmd, "disabled" ) )
1017 schedule->b_enabled = false;
1019 else if( !strcmp( psz_cmd, "date" ) )
1025 time.tm_sec = 0; /* seconds */
1026 time.tm_min = 0; /* minutes */
1027 time.tm_hour = 0; /* hours */
1028 time.tm_mday = 0; /* day of the month */
1029 time.tm_mon = 0; /* month */
1030 time.tm_year = 0; /* year */
1031 time.tm_wday = 0; /* day of the week */
1032 time.tm_yday = 0; /* day in the year */
1033 time.tm_isdst = -1; /* daylight saving time */
1035 /* date should be year/month/day-hour:minutes:seconds */
1036 p = strchr( psz_value, '-' );
1038 if( !strcmp( psz_value, "now" ) )
1040 schedule->i_date = 0;
1050 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1068 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1074 time.tm_mon = i - 1;
1078 time.tm_year = i - 1900;
1079 time.tm_mon = j - 1;
1086 date = mktime( &time );
1087 schedule->i_date = ((mtime_t) date) * 1000000;
1090 else if( !strcmp( psz_cmd, "period" ) )
1094 const char *psz_time = NULL, *psz_date = NULL;
1098 /* First, if date or period are modified, repeat should be equal to -1 */
1099 schedule->i_repeat = -1;
1101 time.tm_sec = 0; /* seconds */
1102 time.tm_min = 0; /* minutes */
1103 time.tm_hour = 0; /* hours */
1104 time.tm_mday = 0; /* day of the month */
1105 time.tm_mon = 0; /* month */
1106 time.tm_year = 0; /* year */
1107 time.tm_wday = 0; /* day of the week */
1108 time.tm_yday = 0; /* day in the year */
1109 time.tm_isdst = -1; /* daylight saving time */
1111 /* date should be year/month/day-hour:minutes:seconds */
1112 p = strchr( psz_value, '-' );
1115 psz_date = psz_value;
1120 psz_time = psz_value;
1123 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1142 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1161 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1162 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1163 schedule->i_period = ((mtime_t) date) * 1000000;
1165 else if( !strcmp( psz_cmd, "repeat" ) )
1169 if( sscanf( psz_value, "%d", &i ) == 1 )
1171 schedule->i_repeat = i;
1178 else if( !strcmp( psz_cmd, "append" ) )
1180 char *command = strdup( psz_value );
1182 TAB_APPEND( schedule->i_command, schedule->command, command );
1192 /*****************************************************************************
1193 * Message handling functions
1194 *****************************************************************************/
1195 vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1197 if( !psz_name ) return NULL;
1199 vlm_message_t *p_message = malloc( sizeof(*p_message) );
1203 p_message->psz_name = strdup( psz_name );
1204 if( !p_message->psz_name )
1209 p_message->psz_value = NULL;
1210 p_message->i_child = 0;
1211 p_message->child = NULL;
1216 vlm_message_t *vlm_MessageNew( const char *psz_name,
1217 const char *psz_format, ... )
1219 vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
1225 assert( psz_format );
1226 va_start( args, psz_format );
1227 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1228 p_message->psz_value = NULL;
1231 if( !p_message->psz_value )
1233 vlm_MessageDelete( p_message );
1239 void vlm_MessageDelete( vlm_message_t *p_message )
1241 free( p_message->psz_name );
1242 free( p_message->psz_value );
1243 while( p_message->i_child-- )
1244 vlm_MessageDelete( p_message->child[p_message->i_child] );
1245 free( p_message->child );
1250 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1251 vlm_message_t *p_child )
1253 if( p_message == NULL ) return NULL;
1257 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1263 /*****************************************************************************
1264 * Misc utility functions
1265 *****************************************************************************/
1266 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1268 vlm_media_t *p_cfg = &p_media->cfg;
1269 vlm_message_t *p_msg;
1270 vlm_message_t *p_msg_sub;
1273 p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1274 vlm_MessageAdd( p_msg,
1275 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1276 vlm_MessageAdd( p_msg,
1277 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1280 vlm_MessageAdd( p_msg,
1281 vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1283 vlm_MessageAdd( p_msg,
1284 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1286 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1287 for( i = 0; i < p_cfg->i_input; i++ )
1290 if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1292 vlm_MessageAdd( p_msg_sub,
1293 vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1298 vlm_MessageAdd( p_msg,
1299 vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1301 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1302 for( i = 0; i < p_cfg->i_option; i++ )
1303 vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1305 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1306 for( i = 0; i < p_media->i_instance; i++ )
1308 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1310 vlm_message_t *p_msg_instance;
1313 if( p_instance->p_input )
1314 var_Get( p_instance->p_input, "state", &val );
1316 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1318 vlm_MessageAdd( p_msg_instance,
1319 vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1320 vlm_MessageAdd( p_msg_instance,
1321 vlm_MessageNew( "state",
1322 val.i_int == PLAYING_S ? "playing" :
1323 val.i_int == PAUSE_S ? "paused" :
1326 /* FIXME should not do that this way */
1327 if( p_instance->p_input )
1329 #define APPEND_INPUT_INFO( key, format, type ) \
1330 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
1331 var_Get ## type( p_instance->p_input, key ) ) )
1332 APPEND_INPUT_INFO( "position", "%f", Float );
1333 APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1334 APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1335 APPEND_INPUT_INFO( "rate", "%f", Float );
1336 APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
1337 APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1338 APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1340 #undef APPEND_INPUT_INFO
1341 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1342 "%d", p_instance->i_index + 1 ) );
1347 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1348 vlm_schedule_sys_t *schedule,
1349 const char *psz_filter )
1353 vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1355 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1359 else if( schedule != NULL )
1363 vlm_message_t *msg_schedule;
1364 vlm_message_t *msg_child;
1367 msg = vlm_MessageSimpleNew( "show" );
1369 vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1371 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1373 vlm_MessageAdd( msg_schedule,
1374 vlm_MessageNew( "enabled", schedule->b_enabled ?
1377 if( schedule->i_date != 0 )
1380 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1382 localtime_r( &i_time, &date);
1383 vlm_MessageAdd( msg_schedule,
1384 vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
1385 date.tm_year + 1900, date.tm_mon + 1,
1386 date.tm_mday, date.tm_hour, date.tm_min,
1390 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1392 if( schedule->i_period != 0 )
1394 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1397 date.tm_sec = (int)( i_time % 60 );
1398 i_time = i_time / 60;
1399 date.tm_min = (int)( i_time % 60 );
1400 i_time = i_time / 60;
1401 date.tm_hour = (int)( i_time % 24 );
1402 i_time = i_time / 24;
1403 date.tm_mday = (int)( i_time % 30 );
1404 i_time = i_time / 30;
1405 /* okay, okay, months are not always 30 days long */
1406 date.tm_mon = (int)( i_time % 12 );
1407 i_time = i_time / 12;
1408 date.tm_year = (int)i_time;
1410 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1411 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1413 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1416 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1418 sprintf( buffer, "%d", schedule->i_repeat );
1419 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1422 vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1424 for( i = 0; i < schedule->i_command; i++ )
1426 vlm_MessageAdd( msg_child,
1427 vlm_MessageSimpleNew( schedule->command[i] ) );
1434 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1436 vlm_message_t *p_msg;
1437 vlm_message_t *p_msg_child;
1438 int i_vod = 0, i_broadcast = 0;
1440 for( int i = 0; i < vlm->i_media; i++ )
1442 if( vlm->media[i]->cfg.b_vod )
1448 p_msg = vlm_MessageSimpleNew( "show" );
1449 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
1450 "( %d broadcast - %d vod )", i_broadcast,
1453 for( int i = 0; i < vlm->i_media; i++ )
1454 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1459 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1463 vlm_message_t *msg_child;
1465 msg = vlm_MessageSimpleNew( "show" );
1466 msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1468 for( i = 0; i < vlm->i_schedule; i++ )
1470 vlm_schedule_sys_t *s = vlm->schedule[i];
1471 vlm_message_t *msg_schedule;
1472 mtime_t i_time, i_next_date;
1474 msg_schedule = vlm_MessageAdd( msg_child,
1475 vlm_MessageSimpleNew( s->psz_name ) );
1476 vlm_MessageAdd( msg_schedule,
1477 vlm_MessageNew( "enabled", s->b_enabled ?
1480 /* calculate next date */
1481 i_time = vlm_Date();
1482 i_next_date = s->i_date;
1484 if( s->i_period != 0 )
1487 while( s->i_date + j * s->i_period <= i_time &&
1493 i_next_date = s->i_date + j * s->i_period;
1496 if( i_next_date > i_time )
1498 time_t i_date = (time_t) (i_next_date / 1000000) ;
1502 strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
1503 localtime_r( &i_date, &tm ) );
1504 vlm_MessageAdd( msg_schedule,
1505 vlm_MessageNew( "next launch", "%s", psz_date ) );
1512 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1514 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1515 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1517 vlm_MessageAdd( show1, show2->child[0] );
1519 /* We must destroy the parent node "show" of show2
1520 * and not the children */
1521 free( show2->psz_name );
1529 return vlm_MessageSimpleNew( "show" );
1533 /*****************************************************************************
1534 * Config handling functions
1535 *****************************************************************************/
1536 static int Load( vlm_t *vlm, char *file )
1541 while( *pf != '\0' )
1543 vlm_message_t *message = NULL;
1546 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1551 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1555 if( pf[i_end] == '\n' ) i_end++;
1558 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1562 if( message->psz_value )
1563 msg_Err( vlm, "Load error on line %d: %s: %s",
1564 i_line, message->psz_name, message->psz_value );
1565 vlm_MessageDelete( message );
1569 if( message ) vlm_MessageDelete( message );
1578 static char *Save( vlm_t *vlm )
1581 char psz_header[] = "\n"
1582 "# VLC media player VLM command batch\n"
1583 "# http://www.videolan.org/vlc/\n\n" ;
1586 int i_length = strlen( psz_header );
1588 for( i = 0; i < vlm->i_media; i++ )
1590 vlm_media_sys_t *media = vlm->media[i];
1591 vlm_media_t *p_cfg = &media->cfg;
1594 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1596 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1598 if( p_cfg->b_enabled )
1599 i_length += strlen( "enabled" );
1601 i_length += strlen( "disabled" );
1603 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1604 i_length += strlen( " loop\n" );
1606 i_length += strlen( "\n" );
1608 for( j = 0; j < p_cfg->i_input; j++ )
1609 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1611 if( p_cfg->psz_output != NULL )
1612 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1614 for( j = 0; j < p_cfg->i_option; j++ )
1615 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1617 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1618 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1621 for( i = 0; i < vlm->i_schedule; i++ )
1623 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1625 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1627 if( schedule->b_enabled )
1629 i_length += strlen( "date //-:: enabled\n" ) + 14;
1633 i_length += strlen( "date //-:: disabled\n" ) + 14;
1637 if( schedule->i_period != 0 )
1639 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1640 strlen( "period //-::\n" ) + 14;
1643 if( schedule->i_repeat >= 0 )
1647 sprintf( buffer, "%d", schedule->i_repeat );
1648 i_length += strlen( "setup repeat \n" ) +
1649 strlen( schedule->psz_name ) + strlen( buffer );
1656 for( j = 0; j < schedule->i_command; j++ )
1658 i_length += strlen( "setup append \n" ) +
1659 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1664 /* Don't forget the '\0' */
1666 /* now we have the length of save */
1668 p = save = malloc( i_length );
1669 if( !save ) return NULL;
1672 p += sprintf( p, "%s", psz_header );
1674 /* finally we can write in it */
1675 for( i = 0; i < vlm->i_media; i++ )
1677 vlm_media_sys_t *media = vlm->media[i];
1678 vlm_media_t *p_cfg = &media->cfg;
1681 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1683 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1685 if( p_cfg->b_enabled )
1686 p += sprintf( p, "enabled" );
1688 p += sprintf( p, "disabled" );
1690 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1691 p += sprintf( p, " loop\n" );
1693 p += sprintf( p, "\n" );
1695 for( j = 0; j < p_cfg->i_input; j++ )
1696 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1698 if( p_cfg->psz_output )
1699 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1701 for( j = 0; j < p_cfg->i_option; j++ )
1702 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1704 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1705 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1708 /* and now, the schedule scripts */
1709 for( i = 0; i < vlm->i_schedule; i++ )
1711 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1713 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1715 localtime_r( &i_time, &date);
1716 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1718 if( schedule->b_enabled )
1720 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1721 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1722 date.tm_hour, date.tm_min, date.tm_sec );
1726 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1727 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1728 date.tm_hour, date.tm_min, date.tm_sec);
1731 if( schedule->i_period != 0 )
1733 p += sprintf( p, "setup %s ", schedule->psz_name );
1735 i_time = (time_t) ( schedule->i_period / 1000000 );
1737 date.tm_sec = (int)( i_time % 60 );
1738 i_time = i_time / 60;
1739 date.tm_min = (int)( i_time % 60 );
1740 i_time = i_time / 60;
1741 date.tm_hour = (int)( i_time % 24 );
1742 i_time = i_time / 24;
1743 date.tm_mday = (int)( i_time % 30 );
1744 i_time = i_time / 30;
1745 /* okay, okay, months are not always 30 days long */
1746 date.tm_mon = (int)( i_time % 12 );
1747 i_time = i_time / 12;
1748 date.tm_year = (int)i_time;
1750 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1751 date.tm_year, date.tm_mon, date.tm_mday,
1752 date.tm_hour, date.tm_min, date.tm_sec);
1755 if( schedule->i_repeat >= 0 )
1757 p += sprintf( p, "setup %s repeat %d\n",
1758 schedule->psz_name, schedule->i_repeat );
1762 p += sprintf( p, "\n" );
1765 for( j = 0; j < schedule->i_command; j++ )
1767 p += sprintf( p, "setup %s append %s\n",
1768 schedule->psz_name, schedule->command[j] );
1776 #endif /* ENABLE_VLM */